程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> SqlServer數據庫 >> 關於SqlServer >> SQL server 表數據改變觸發發送郵件的方法

SQL server 表數據改變觸發發送郵件的方法

編輯:關於SqlServer

今天遇到一個問題,原有生產系統正在健康運行,現需要監控一張數據表,當增加數據的時候,給管理員發送郵件。

領到這個需求後,有同事提供方案:寫觸發器觸發外部應用程序。這是個大膽的想法啊,從來沒寫過這樣的觸發器。

以下是參考文章:

第一種方法: 觸發器調用外部程序。 xp_cmdshell

http://www.jb51.net/article/90714.htm 第一篇提供的方法是需要開啟xp_cmdshell

先開啟xp_cmdshell

打開外圍應用配置器—>

功能的外圍應用配置器—>

實例名\Database Engine\xp_cmdshell—>

啟用

然後可以調用外部程序:Exec xp_cmdshell 'c:\calc.exe' 。

第二種方法:將插入的值傳給.bat 。同樣使用調用外部程序的 xp_cmdshell 的權限

http://www.sqlparty.com/%E9%A2%98%E7%82%BC/2013/08/05/e5-a6-82-e4-bd-95-e5-9c-a8-e6-9f-90-e8-a1-a8-e6-96-b0-e6-8f-92-e5-85-a5-e6-95-b0-e6-8d-ae-e6-97-b6-e8-a7-a6-e5-8f-91-e6-89-a7-e8-a1-8c-e5-a4-96-e9-83-a8-e7-a8-8b-e5-ba-8f-ef-bc-9f.html

其實第二種方法可以歸為第一種。

下面說說第三種方法:

SQL servere CLR

這種方法可以利用VS給sql server 寫存儲過程和觸發器。打開了VS不愁寫代碼給管理員發email。

第四種方法:

SQL server Management -->Database Mail

開啟Database Mail 之後,配制好發郵件的設置,直接寫觸發器就可以把插入的內容通過sql server 發送出來了。

Create TRIGGER tri_email 
ON [dbo].[ImageGalleries]
AFTER insert
AS
BEGIN
if exists(select * from inserted)
begin
declare @content nvarchar(max)
select @content=i.Name+'|'+i.ImagePath from inserted i;
exec msdb.dbo.sp_send_dbmail @profile_name='SQLProfile',
@[email protected]',
@subject='sql server send email by trigger',
@body=@content
end
END
GO 

這種方法是最直接最簡便的方法。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved