jeudi 12 mars 2020

T-SQL - IF true then run sp_send_dbmail ELSE do nothing?

I've tried several different approaches based on posts Google spit out. But I just can't get it to work. I'm running a select statement and the results are being emailed to the recipient by using sp_send_dbmail. This works perfectly fine. What I want to accomplish, is that no email goes out if the select statement comes back with 0 rows. I've tried many things, one example:

    Select Statement    --The select statement is at the top of the code
    IF @@rowcount = 0     -- followed by the IF statement
    begin
    return
    end
    ELSE
    begin
    exec msdb.dbo.sp_send_dbmail 
    end

Running above example, I do not get an error, but in either case, 0 or not 0, it sends the email off.

how do I encapsulate the sp.send_dbmail into an IF statement based on if a select statement comes back with zero results?

Thanks!

Aucun commentaire:

Enregistrer un commentaire