THE SQL Server Blog Spot on the Web

Welcome to - The SQL Server blog spot on the web Sign in | |
in Search

Maria Zakourdaev

Alert visualization recipe: Get out your blender, drop in some sp_send_dbmail, Google Charts API, add your favorite colors and sprinkle with html. Blend till it’s smooth and looks pretty enough to taste.


I really like database monitoring. My email inbox have a constant flow of different types of alerts coming from our production servers with all kinds of information, sometimes more useful and sometimes less useful. Usually database alerts look really simple, it’s usually a plain text email saying “Prod1 Database data file on Server X is 80% used. You’d better grow it manually before some query triggers the AutoGrowth process”.

Imagine you could have received email like the one below.  In addition to the alert description it could have also included the the database file growth chart over the past 6 months. Wouldn’t it give you much more information whether the data growth is natural or extreme? That’s truly what data visualization is for.

Believe it or not, I have sent the graph below from SQL Server stored procedure without buying any additional data monitoring/visualization tool.



Would you like to visualize your database alerts like I do? Then like myself, you’d love the Google Charts.

All you need to know is a little HTML and have a mail profile configured on your SQL Server instance regardless of the SQL Server version.

First of all, I hope you know that the sp_send_dbmail procedure has a great parameter @body_format = ‘HTML’, which allows us to send rich and colorful messages instead of boring black and white ones. All that we need is to dynamically create HTML code.

This is how, for instance, you can create a table and populate it with some data:

DECLARE @html varchar(max)

SET @html = '<html>'
+ '<H3><font id="Text" style="color: Green;">Top Databases: </H3>'
+ '<table border="1" bordercolor="#3300FF" style="background-color:#DDF8CC" width="70%" cellpadding=3 cellspacing=3>'
+ '<tr><font color="Green"><th>Database Name</th><th>Size</th><th>Physical Name</th></tr>' +

                            td = name,'',
                            td = size * 8/1024 ,'',
                            td = physical_name 
            FROM sys.master_files  
            ORDER BY size DESC
            FOR XML PATH ('tr'),TYPE

EXEC msdb.dbo.sp_send_dbmail
@recipients = 'PutYourEmailHere',
@subject ='Top databases',
@body = @html,
@body_format = 'HTML'

This is the result:



If you want to add more visualization effects, you can use Google Charts Tools which is a free and rich library of data visualization charts, they’re also easy to populate and embed.

There are two versions of the Google Charts

image Image based charts:

This is an old version, it’s officially deprecated although it will be up for a next few years or so. I really enjoy using this one because it can be viewed within the email body. For mobile devices you need to change the “Load remote images” property in your email application configuration.        


image Charts based on JavaScript classes:

This API is newer, with rich and highly interactive charts, and it’s much more easier to understand and configure. The only downside of it is that they cannot be viewed within the email body. Outlook, Gmail and many other email clients, as part of their security policy, do not run any JavaScript that’s placed within the email body. However, you can still enjoy this API by sending the report as an email attachment.

Here is an example of the old version of Google Charts API, sending the same top databases report as in the previous example but instead of a simple table, this script is using a pie chart right from  the T-SQL code

DECLARE @html  varchar(8000)

DECLARE @Series  varchar(800),@Labels  varchar(8000),@Legend  varchar(8000);
SET @Series = '';
SET @Labels = '';
SET @Legend = '';

SELECT TOP 5 @Series = @Series + CAST(size * 8/1024 as varchar) + ',',
                        @Labels = @Labels +CAST(size * 8/1024 as varchar) + 'MB'+'|',
                        @Legend = @Legend + name + '|'
FROM sys.master_files

SELECT @Series = SUBSTRING(@Series,1,LEN(@Series)-1),
        @Labels = SUBSTRING(@Labels,1,LEN(@Labels)-1),
        @Legend = SUBSTRING(@Legend,1,LEN(@Legend)-1)

SET @html =
  '<H3><font color="Green"> '+@@ServerName+' top 5 databases : </H3>'+
   '<img src="'+
   'alt="'+@@ServerName+' top 5 databases" />'
EXEC msdb.dbo.sp_send_dbmail @recipients = 'PutYourEmailHere',
                            @subject = 'Top databases',
                            @body = @html,
                            @body_format = 'HTML'

This is what you get. Isn’t it great?


Chart parameters reference:

chf     Gradient fill  bg - backgroud ; s- solid
cht     chart type  ( p - pie)
chs        chart size width/height
chco    series colors
chd        chart data string        1,2,3,2
chl        pir chart labels        a|b|c|d
chma    chart margins
chdl    chart legend            a|b|c|d
chdlp    chart legend text        b - bottom of chart


Line graph implementation is also really easy and powerful

DECLARE @html varchar(max)
DECLARE @Series varchar(max)
DECLARE @HourList varchar(max)

SET @Series = '';
SET @HourList = '';

SELECT @HourList = @HourList + SUBSTRING(CONVERT(varchar(13),last_execution_time,121), 12,2)  + '|' ,
             @Series = @Series + CAST( COUNT(1) as varchar) + ','
FROM sys.dm_exec_query_stats s 
   CROSS APPLY sys.dm_exec_sql_text(plan_handle) t
WHERE last_execution_time > = getdate()-1
GROUP BY CONVERT(varchar(13),last_execution_time,121)
ORDER BY CONVERT(varchar(13),last_execution_time,121)

SET @Series = SUBSTRING(@Series,1,LEN(@Series)-1)

SET @html =
'<img src="'+
'chdl= Proc executions from cache&'+
'chtt=Executions for from'
+(SELECT CONVERT(varchar(16),min(last_execution_time),121)
         FROM sys.dm_exec_query_stats
         WHERE last_execution_time > = getdate()-1)
+' till '+
+(SELECT CONVERT(varchar(16),max(last_execution_time),121)
    FROM sys.dm_exec_query_stats)
'chma=55,120,0,0" alt="" />'

EXEC msdb.dbo.sp_send_dbmail
@recipients = 'PutYourEmailHere',
@subject ='Daily number of executions',
@body = @html,
@body_format = 'HTML'


Chart parameters reference:

chco    series colors
chd        series data
chds    scale format
chdl    chart legend
chf        background fills
chg        grid line
chls    line style
chm        line fill
chs        chart size
cht        chart type
chts    chart style
chtt    chart title
chxp    axis label positions
chxs    axis label styles
chxt    axis tick mark styles
chxl    axis labels
chma    chart margins

If you don’t mind to get your charts as an email attachment, you can enjoy the Java based Google Charts which are even easier to configure, and have much more advanced graphics. In the example below, the sp_send_email procedure uses the parameter @query which will be executed at the time that sp_send_dbemail is executed and the HTML result of this execution will be attached to the email.

DECLARE @html varchar(max),@query varchar(max)
DECLARE @SeriesDBusers  varchar(800);
SET @SeriesDBusers = '';

SELECT @SeriesDBusers = @SeriesDBusers +  ' ["'+DB_NAME(r.database_id) +'", ' +cast(count(1) as varchar)+'],'
FROM sys.dm_exec_requests r
GROUP BY DB_NAME(database_id)
ORDER BY count(1) desc;

SET @SeriesDBusers = SUBSTRING(@SeriesDBusers,1,LEN(@SeriesDBusers)-1)

SET @query = '
    <script type="text/javascript" src=""></script>
    <script type="text/javascript">
      google.load("visualization", "1", {packages:["corechart"]});
      function drawChart() {
                     var data = google.visualization.arrayToDataTable([
                       ["Database Name", "Active users"],
                     var options = {
                       title: "Active users",
                       pieSliceText: "value"
                     var chart = new google.visualization.PieChart(document.getElementById("chart_div"));
                     chart.draw(data, options);
        <div id="chart_div" style="width: 800px; height: 300px;"></div>

EXEC msdb.dbo.sp_send_dbmail
   @recipients = 'PutYourEmailHere',
   @subject ='Active users',
   @body = @html,
   @body_format = 'HTML',
   @query = @Query
   @attach_query_result_as_file = 1, 
   @query_attachment_filename = 'Results.htm'

After opening the email attachment in the browser you are getting this kind of report:


In fact, the above is not only for database alerts. It can be used for applicative reports if you need high levels of customization that you cannot achieve using standard methods like SSRS.

If you need more information on how to customize the charts, you can try the following:

Use the above examples as a starting point for your procedures and I’d be more than happy to hear of your implementations of the above techniques.



Published Wednesday, June 26, 2013 6:08 PM by Maria Zakourdaev
Filed under: ,

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS



Mike Lawell said:

Very cool, thanks Maria

June 26, 2013 11:06 AM

spaghettidba said:

Absolutely awesome! Thanks for sharing.

June 26, 2013 11:50 AM

Lazydba247 said:

Thanks for sharing!

June 27, 2013 4:04 AM

Argenis Fernandez said:

Thanks for this tip! Awesome idea.

June 27, 2013 1:12 PM

Maria Zakourdaev said:

Mike, Spaghettidba, Lazydba247, Argenis, many thanks for your feedback. I'm really happy that you liked the idea!!  

June 27, 2013 3:12 PM

Kevin Lan said:

very cool, good point

June 27, 2013 11:42 PM

Kevin Kline said:

Very good blog post, Maria. Glad to see you've started writing such top-quality content!  -Kev

June 28, 2013 9:35 AM

Maria Zakourdaev said:

Thank you, Kevin!

June 28, 2013 10:19 AM

Neeraj said:

Nice.. Going to use this in my alert. Thanks Maria !!

July 23, 2013 2:29 PM

Maria Zakourdaev said:

Excellent, Neeraj, I'm glad that you are going to use it!

July 24, 2013 5:19 AM

Tadeusz said:

That's simply brilliant!

August 26, 2013 9:33 AM

Stephane said:

exactly what I was trying to archieve !

thank you Maria.

September 3, 2013 2:43 AM

Maria Zakourdaev said:

Tadeusz and Stephane: huge thanks for leaving such a positive feedback! I'm glad that you will be using this technique.

September 3, 2013 3:40 AM

Sam said:


July 2, 2014 1:58 PM

Maria Zakourdaev said:

One more report using Google Charts:
September 8, 2014 7:01 AM

George said:

Excellent work Maria, It helped me alot after months of search

March 28, 2016 4:15 AM

Pete said:

Unbelievable stuff!

Thanks for putting this together and sharing it!

April 28, 2017 2:00 AM

Pete said:

I was also wondering if these graphs can be saved as images somewhere using SQL?

April 28, 2017 2:06 AM

Wim said:

Best post ever, just what I'm looking for.

Was already using html tables but this ROCKS!

Many Thanks Maria!

September 11, 2017 5:47 AM

Maria Zakourdaev said:

Pete, you can send yourself html file with the report or save it on the server or any shared storage.

December 17, 2017 6:59 AM

Pitt said:

so cool , Looking for this .Thanks so much

June 30, 2018 10:59 PM

Leave a Comment


About Maria Zakourdaev

The shortest word in the English language that contains the letters: abcdef is… feedback! Let me know if I have touched something that is alive in the cosmos.
Privacy Statement