如果使用sp_send_dbmail

时间:2017-10-02 14:34:15

标签: sql sql-server-2008

如果没有可用的结果,我希望发送一条特定的消息。现在,如果我的工作已执行且没有任何报告,则电子邮件只显示空白。

SP低于

EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'Mail_profile',
@recipients = 'myemail@somename.com',
@subject = 'Weekly',
@query = N'EXEC PTW.dbo.SP_Weekly',
@attach_query_result_as_file = 1,
@query_result_width = 150,
@query_result_header= 1,
@query_attachment_filename = 'Weekly.txt',
@importance = 'High',
@query_result_no_padding = 1,
@query_result_separator = ' ';

如果在运行此选项时没有可用的记录,我想显示No records available at this time

我试图在ISNULL之前插入@query,但这是我难以接受的。有什么建议吗?

此信息也会始终显示No records found for 'Yesterdays date'

思维ID必须在那里的某处添加dateadd(day, datediff(day, 0, getdate()), -1),但在哪里?

1 个答案:

答案 0 :(得分:1)

declare @subject1  varchar(max)
declare @body1 varchar(max)
EXEC PTW.dbo.SP_Weekly
if @@rowcount=0
begin
set @subject1='No data or any message that suits you'
set @body1='some message'
EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'Mail_profile',
@recipients = 'myemail@somename.com',
@subject = @subject1,
@importance = 'High',
@body=@body1,
@query_result_separator = ' ';
return
end

else
begin
EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'Mail_profile',
@recipients = 'myemail@somename.com',
@subject = 'Weekly,
@query = N'EXEC PTW.dbo.SP_Weekly',
@attach_query_result_as_file = 1,
@query_result_width = 150,
@query_result_header= 1,
@query_attachment_filename = 'Weekly.txt',
@importance = 'High',
@query_result_no_padding = 1,
@query_result_separator = ' ';
end