当SQL Server jobs失败时自动发送邮件

来源:互联网 发布:游戏下载软件 编辑:程序博客网 时间:2024/06/11 06:29

需要三个步骤:

1,配置SQL Agent Mail。

http://msdn.microsoft.com/en-us/library/ms186358.aspx

在这个步骤中会配置profile,profile的email地址就是之后发送邮件的发件人。

2,创建Operator。

http://msdn.microsoft.com/en-us/library/ms175962%28v=sql.90%29.aspx

Operator的邮件地址就是之后发送邮件的收件人。如果有多个收件人,邮箱地址用分号分割。

3,在job中设置Notification。

右键点击job->properties->notification   配置当job失败时,发送邮件。


一切配置完成后,为了测试创建一个肯定会失败的job,例如查询一个不存在的表。job运行失败后,查询job history你会发现error:

"NOTE: Failed to notify 'operator name' via email."


这是因为SQL Agent需要enable mail profile. 

Right click SQL Server Agent -> properties -> Alert System -> enable checkbox of "Enable mail profile".  Then restart SQL Server Agent you will receive emails when jobs fail.


所有配置过程还可以全部用T-SQL来实现,配置大量机器时非常实用哦~~:

http://www.snapdba.com/2013/04/enabling-and-configuring-database-mail-in-sql-server-using-t-sql/