即使条件为false,sp_send_dbmail也会发送邮件 - SQL

时间:2014-04-22 13:35:06

标签: sql select sendmail

我的IF语句是存储过程的一部分,如下所示:

BEGIN TRY

IF EXISTS (SELECT which I have confirmed returns no results, has a variable called from another table in the where clause)
    BEGIN           
            EXEC msdb.dbo.sp_send_dbmail 
                @profile_name   = @dynamic_profile, 
                @recipients     = @dynamic_recipient,
                @subject        = @subjectline,
                @body           = @mailHTML,   
                @body_format    = 'HTML';

    END     
END TRY

这与每15分钟运行一次的工作有关。 为什么在IF语句要求跳过邮件发送时,如果select没有返回结果,它会不断发送空白电子邮件?

我的NOCOUNT设为开启

1 个答案:

答案 0 :(得分:2)

尝试 - 改为:

 IF (SELECT COUNT(*) FROM whatever) > 0
 BEGIN

  --MAIL

 END