如何每天将sql查询或存储过程输出的结果发送到特定电子邮件。我在Windows 2008 Server上安装了Microsoft SQL Server 2008 R2。
我的查询是这样的:
SELECT Ename, COUNT(*) TotalCount
FROM emp
GROUP BY Ename
HAVING COUNT(*) > 1
每当查询返回TotalCount的结果时,Sql server应该发送邮件。
答案 0 :(得分:1)
假设您已设置DBMail,简单的方法是将此脚本放入SQL代理作业的步骤中:
EXEC msdb.dbo.sp_send_dbmail
@recipients = 'email@address.com',
@query = 'SELECT Ename, COUNT(*) TotalCount
FROM emp
GROUP BY Ename
HAVING COUNT(*) > 1' ,
@subject = 'Count of Employee Names',
@attach_query_result_as_file = 0