Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Running a select statement located in local variable 1

Status
Not open for further replies.

tvbruwae

Programmer
Aug 9, 2001
224
EU
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
 
SET @Query = YourQueryString
EXEC(@Query)
IF @@ROWCOUNT > 0
etc. JHall
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top