John McCormack DBA

SQL Server Databases and Cloud

  • Personal
    • About
  • Free Training
    • SQL Server on Amazon RDS (Free Course)
    • Free practice questions to help you pass DP-900
  • Save money in Azure
    • Azure IaaS SQL Backups – Stop burning money
    • Your Azure SQL Database and Managed Instance is too big
    • Turn the cloud off at bedtime to save 70%
    • Your Azure SQL Virtual Machine might be too big
    • Save money with Azure SQL DB serverless
    • Save up to 73% with reserved instances
    • Delete unused instances to save money in Azure
  • Hire me
    • 60 minute cost optimization
    • Let me solve your SQL Server problems
    • Take a look at my Sessionize speaker’s profile

Send T-SQL query results in a HTML table

7th August 2014 By John McCormack Leave a Comment

Send T-SQL query results in a HTML table

If you need to email out t-sql query results in a HTML table, whether on an ad-hoc basis or regularly via a job, it can be useful to present the results in a HTML formatted table. This makes the output much easier to read for the recipients, especially if they are not used to using management studio and seeing unformatted data.

This simple script shows you how to do this. I have used the Adventureworks2012 database for the example, all that needs to be done to make it relevant to your real data is to update the query part and table headers as well as other text fields. You can also change the css if you know what you are doing to use different colours, fonts and other styles.

/***************************************************************************************

* Send email script

****************************************************************************************/
DECLARE @EmailRecipient VARCHAR(100), @SubjectText VARCHAR(100), @ProfileName VARCHAR(100), @Body VARCHAR(MAX)
SELECT TOP 1 @EmailRecipient = 'testemail@gmail.com'
SELECT @SubjectText = 'Top 10 Sales YTD'
SELECT TOP 1 @ProfileName = name FROM msdb.dbo.sysmail_profile

SET @Body =

N'<H4 style="color:#000080; font-family:Arial,Verdana">AdventureWorks2012</H4>' +
N'<H5 style="color:#000080; font-family:Arial,Verdana">Top 10 Sales Year to date</H5>' +
N'<table border="3"; style="font-family:Arial,Verdana; text-align:left; font-size:9pt; color:#000033">' +

N'<tr style="text-align:left;">
<th style="text-align:left;background-color:#000080;color:#FFF;font-weight:bold; width:50%;">BusinessEntityID</th>
<th style="text-align:left;background-color:#000080;color:#FFF;font-weight:bold;width:25%;">SalesYTD</th>
<th style="text-align:left;background-color:#000080;color:#FFF;font-weight:bold;width:25%;">SalesLastYear</th>'
+
CAST ( ( SELECT TOP 10
td = [BusinessEntityID] , '',
td = [SalesYTD] , '',
td = [SalesLastYear]
FROM [AdventureWorks2012].[Sales].[SalesPerson] sp
ORDER BY SalesYTD DESC
FOR XML PATH('tr'), TYPE
) AS NVARCHAR(MAX) ) +
N'</table>'

EXEC msdb.dbo.sp_send_dbmail
@profile_name = @ProfileName,
@recipients = @EmailRecipient,
@body_format = 'HTML',
@body = @Body,
@subject = @SubjectText;

/**************************************************************************************/

If you are going to be scheduling a job to send the email regularly, you might want to wrap some logic around it in case there is no data to report. This is particularly useful for error reporting for DBAs where you want to be alerted of errors but only when there are any, you don’t want a regular blank email.

/***************************************************************************************
* Adding logic script
****************************************************************************************/
IF EXISTS (SELECT 1 FROM [AdventureWorks2012].[Sales].[SalesPerson])
BEGIN
-- Put your code in here
END
/***************************************************************************************/

Share this:

  • Tweet
  • Email

Related

Filed Under: T-SQL Tagged With: database mail, email db query, email out database query, email t-sql query result, send html email from sql server, SQL server, SQL Server 2008, SQL Server 2012, t-sql

About John McCormack

John McCormack is an experienced SQL DBA with extensive knowledge of the two largest public clouds: AWS and Azure.

Leave a ReplyCancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Privacy & Cookies: This site uses cookies. By continuing to use this website, you agree to their use.
To find out more, including how to control cookies, see here: Cookie Policy

John McCormack · Copyright © 2025