Sql server Db Mail which can send mail for two or more queries

Bhaskar Mishra picture Bhaskar Mishra · May 9, 2013 · Viewed 11k times · Source

I want to send db mail , which contains the result of two queries, how do I make it happen?

USE msdb
EXEC sp_send_dbmail
  @profile_name = 'try01',
  @recipients = '[email protected]',
  @subject = 'Table Values',
  @body = 'xxxxxxxxxxxxxx.',
  @execute_query_database = 'Reports',
  @query = 'SELECT * from Table'

Answer

Bhaskar Mishra picture Bhaskar Mishra · May 10, 2013

OK, This was easy as if you put a semicolon the result of both the queries are sent. Here it is:

USE msdb
EXEC sp_send_dbmail
  @profile_name = 'try01',
  @recipients = '[email protected]',
  @subject = 'Table Values',
  @body = 'xxxxxxxxxxxxxx.',
  @execute_query_database = 'Reports',
  @query = 'SELECT * from Table; select * from table2'

Only problem I have now is to get this into a decent looking format.