Hi
I'm in the following situation : I need SQL Server to send a mail to the users (with xp_sendmail) when no records from a certain select statement are returned. It's easy to write it all out and make it work, but I guess there's a way to run a select statement located in a local variable? This way, I could write something like this :
@Query = 'Select * from MyTable where...'
if <The query in @query returns one or more records>
master.dbo.xp_sendmail @Message, @Subject, @Query
else
<do nothing>
Like I say, it can be done by writing the query twice or by using a cursor, but I don't know SQL well enough to write it more compact. Can this be done?
Thanks!
Tim
I'm in the following situation : I need SQL Server to send a mail to the users (with xp_sendmail) when no records from a certain select statement are returned. It's easy to write it all out and make it work, but I guess there's a way to run a select statement located in a local variable? This way, I could write something like this :
@Query = 'Select * from MyTable where...'
if <The query in @query returns one or more records>
master.dbo.xp_sendmail @Message, @Subject, @Query
else
<do nothing>
Like I say, it can be done by writing the query twice or by using a cursor, but I don't know SQL well enough to write it more compact. Can this be done?
Thanks!
Tim