Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Multiple queries within sp_send_dbmail ?

Author  Topic 

swims01
Yak Posting Veteran

59 Posts

Posted - 2009-05-22 : 13:03:23
Hey all.

I'm having a lot of success with all of my queries and sending results through a SQL job. The jobs send HTML emails and all is working pretty well.

Unfortunately I have a number of queries and they are all very large.

Is it possible to declare more than one variable to hold query data and then reference them within the job?

Example....
DECLARE @MyQuery1 varchar(max)
DECLARE @MyQuery2 varchar(max)

SET @MyQuery1 = <query>
SET @MyQuery2 = <query>

EXEC msdb.dbo.sp_send_dbmail
@recipients=N'me@me.com',
@body=@MyQuery1 + @MyQuery2,
@subject ='Subject',
@profile_name ='ProfileName',
@execute_query_database = 'db',
@body_format = 'HTML'

swims01
Yak Posting Veteran

59 Posts

Posted - 2009-05-22 : 13:16:49
Also, I think there might be a max amount of characters that can be within a SQL job which appears to be my real issue here....
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-05-23 : 11:40:31
why dont you put results of queries in a procedure and then just use exec procedure as the query for sp_send_dbmail?
Go to Top of Page

swims01
Yak Posting Veteran

59 Posts

Posted - 2009-05-28 : 16:41:13
So, I'd create a stored procedure using both queries in the one procedure?
and then...
@body=@MyStoredProcedure
?
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-05-29 : 14:20:10
the query will be EXEC YourProcedure param1,param2,...
Go to Top of Page
   

- Advertisement -