Categories
Sql Server

Send SQL SERVER Query Result by Database Mail

Send SQL SERVER Query Result by Database Mail

Hi friends in this topic we will learn how can we send query results through database mail in SQL SERVER, or you can say also how to Send SQL SERVER Query Result by Database Mail.

Earlier we had discussed SQL Query Result set to Html Table.

My manager told me to that configure a job that will send a particular SQL SERVER query result to some senior management guys, so I start exploring possible way and best of them also. I know it can be done but how is the main concern.

So by using database mail and it’s built in feature sp_send_dbmail. I have done it.

SCRIPT:-

------ Start T-SQL------
 
    USE msdb
    EXEC sp_send_dbmail
    
   --SQL SERVER DATABASE MAIL PROFILE NAME--
    @profile_name = 'sql mail',
   
   --RECIPIENTS MAIL IS--
    @recipients = 'mandeep@hightechnology.in',
   
   --MAIL SUBJECT--
    @subject = 'T-SQL Query Result',
    @body = 'The result from SELECT is appended below.',
   
   --QUERY WILL RUN ON WHICH DATABASE--
    @execute_query_database = 'Dummy',
   
   --QUERY WHICH RESULT YOU WANT TO SEND OVER MAIL--
    @query = 'select top 10 * from address'
 
------ End T-SQL --  ----
 
 

Note:- For this query Database Mail feature should be enabled and configured in SQL SERVER.As of now this query result will be shown in mail body and in case of large data, it looks weird. I will post another post so that query result have some better UI.