0

How can i pass multiple query in a @query parameter in sp_send_dbmail?

For example:

select count(*) from TableA
IF count(*)/ @@rowcount = 0  
Exec sp_send_dbmail @profile_name = xx, @recipients = '[email protected]',@subject = 'Test', @body= ' No rows';
IF count(*)/ @@rowcount > 0
Exec sp_send_dbmail @profile_name = xx, @recipients = '[email protected]',@subject = 'Test', 
@body= ' xx rows';

I am not getting any error message, but it stops after the first select statement.

1 Answer 1

1

When you do the select count(*) from TableA you do not keep the value somewhere, it only execute and return the result from your query. This why the if statements are not firing. You could do something like this:

DECLARE @c AS int
SET @c = (select count(*) from TableA)

IF @c = 0
Exec sp_send_dbmail @profile_name = xx, @recipients = '[email protected]',@subject = 'Test', @body= 'No rows';

IF @c > 0
Exec sp_send_dbmail @profile_name = xx, @recipients = '[email protected]',@subject = 'Test', 
@body= ' xx rows';

Though I have some reservations about putting this kind of logic on the SQL side. But since I don't know how you are using this, it should work for now.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.