Quantcast
Channel: SQL Server Database Engine forum
Viewing all articles
Browse latest Browse all 12963

Get query result in E-mail

$
0
0

Hi Everyone,

I am trying to get SQL Query Result in e-mail with proper format.

I tired DB mail but result is not in proper format . It's hard to read from attachment. 

From Below Query HTML format showing first 3 columns good after that all getting mixed up. 

Select Ticktsno,customer_Id,Phone,Open date-time,OpenedBy,Assign_to,Category,Support_level,Incident_type,Description from dbo.query_result. 

HTML Code: 

                                   


 if @@ROWCOUNT > 0
begin
declare @emailSubject varchar(100),
  @columnHeaders varchar(1000),
  @tableHTML nvarchar(max)

select @emailSubject = 'My Results Email',      -- Subject of Email
  @columnHeaders = 'tickets</th><th>Customer</th><th>Phone</th><th>Open Date/Time</th><th>OpenedBy</th><th>Assign_To</th><th>Category</th><th>Support_Level</th><th>Incident_Type</th><th>Status</th><th>DEscription'   -- Column headers (must put </th><th> between each header)

set @tableHTML =
  '<div><b>My Email Header</b></div><br>' + -- This is the bold text at the top of your email
  '<table border="1" cellpadding="5"><font face="Calibri" size=2>' +
  '<tr><th>' + @columnHeaders + '</th></tr>' +
  convert(nvarchar(max),
  (
   SELECT td =  Tickets,       '',
                    td =  Customer, '',
                    td =  Phone, '',
                    td = [Open Date/Time], '',
                    td = OpenedBy, '',
                    td = Assign_To,
                    td = Category,
                    td =  Support_Level,
                    td = Incident_Type ,
td = [Description]         

                     from master.dbo.QueryResult_2000
                      
   for xml path('tr'), type))

  +'</font></table>'

EXEC msdb.dbo.sp_send_dbmail @recipients='xxx@yy.com',
    @subject = 'Query Result',
    @body = @tableHTML,
    @body_format = 'HTML' ;
end

What changes should make in HTML code to get report in proper HTML  format


Please Mark As Answer if it is helpful. \\Aim To Inspire Rather to Teach A.Shah



Viewing all articles
Browse latest Browse all 12963

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>