本文转自:http://blog.netnerds.net/2008/02/create-a-basic-sql-server-2005-trigger-to-send-e-mail-alerts/

For as many times as I have read about sending e-mails using SQL Server triggers, I've rarely come across actual code samples. After someone asked for a "Triggers for Dummies" example in a Facebook SQL group, I created the following example which uses a trigger to alert a manager that an expensive item has been entered into inventory.

First, if SQL Mail isn't enabled and a profile hasn't been created, we must do so.

--// First, enable SQL SMail
use master
go
 
sp_configure 'show advanced options',1
go
 
reconfigure with override
go
 
sp_configure 'Database Mail XPs',1
go
 
reconfigure
go
--//Now create the mail profile.
--//CHANGE @email_address,@display_name and @mailserver_name VALUES to support your environment
EXECUTE msdb.dbo.sysmail_add_account_sp
@account_name = 'DBMailAccount',
@email_address = 'sqlserver@domain.com',
@display_name = 'SQL Server Mailer',
@mailserver_name = 'exchangeServer'

EXECUTE msdb.dbo.sysmail_add_profile_sp
@profile_name = 'DBMailProfile'

EXECUTE msdb.dbo.sysmail_add_profileaccount_sp
@profile_name = 'DBMailProfile',
@account_name = 'DBMailAccount',
@sequence_number = 1 ;
 

Now that SQL will support sending e-mails, let's create the sample table.

This is not a useful or well designed table by any means -- it's just a simple example table:

CREATE TABLE dbo.inventory (
item varchar(50), price money
)
GO

Now that SQL mail and the table are setup, we will create a trigger that does the following:

  • Creates an AFTER INSERT trigger named expensiveInventoryMailer on the inventory table.

This means that the trigger will be executed after the data has been entered.

  • Checks for items being entered that have a price of $1000 or more
  • If there is a match, an email is sent using the SQL Mail profile we used above.
CREATE TRIGGER expensiveInventoryMailer
ON dbo.inventory AFTER INSERT
AS
DECLARE @price money
DECLARE @item varchar(50)

SET @price  = (SELECT price FROM inserted)
SET @item = (SELECT item FROM inserted)

IF @price >= 1000
  BEGIN
    DECLARE @msg varchar(500)
    SET @msg = 'Expensive item "' + @item + '" entered into inventory at $' + CAST(@price as varchar(10)) + '.'
    --// CHANGE THE VALUE FOR @recipients
    EXEC msdb.dbo.sp_send_dbmail
        @recipients=N'manager@domain.com',
        @body= @msg, 
        @subject = 'SQL Server Trigger Mail',
        @profile_name = 'DBMailProfile'
   END
GO

The only way to test a trigger is to add actual data, so let's do that here:

insert into inventory (item,price) values ('Vase',100)

insert into inventory (item,price) values ('Oven',1000)

Your email should arrive very quickly.

If it doesn't, check the SQL Server mail log in SQL Management Studio by running SELECT * FROM sysmail_allitems.

Have fun!

另:

Database Mail and SQL Mail Stored Procedures (Transact-SQL)

http://msdn.microsoft.com/en-us/library/ms177580(v=sql.90).aspx

最新文章

  1. Google疯了,竟然这样!
  2. 微信支付Native扫码支付模式二之CodeIgniter集成篇
  3. Codeforces Round #383 (Div. 2) D 分组背包
  4. php 截取中文字符串 - ord()函数 0xa0...
  5. LLDB, iOS调试器
  6. UIWebView 操作
  7. Linux常用命令_(安装包管理)
  8. 【转】github更新自己Fork的代码
  9. 2075 yh女朋友的危机、2544 拯救小矮人
  10. 输入参数varargin
  11. centos扩容(pv,vg,lv)
  12. mac下 redis安装使用
  13. (五):C++分布式实时应用框架——微服务架构的演进
  14. hdu 2112 map+Dijkstra
  15. db2笔记
  16. java监控指定路径下文件及文件夹变化
  17. Linux 配置selenium + webdriver 环境
  18. 关于jQuery Form Plugin使用心得
  19. GeSHi——通用语法高亮显示
  20. 让Android 变身回一台真正的Linux系统吧!!!

热门文章

  1. DRF的@action装饰器
  2. git学习资源合集
  3. 服务器迁移到 Windows Servec 2008 R2 64 bit 和IIS 7问题记录
  4. python中文转换url编码
  5. 并发系列6-Java并发面试系列文章总结【石杉的架构笔记】
  6. HDU6058 Kanade's sum(思维 链表)
  7. 03、NavMesh--导航网格寻路
  8. Codeforces Testing Round 14
  9. Gym 101128F Sheldon Numbers(网络流)
  10. 【Manacher算法】poj3974 Palindrome