How to send sql query results ina tablular form through email in sql server 2005

Posted on 2011-10-26
Last Modified: 2012-05-12
Hello Experts,

I have a query whose results i have to display in the email not attachment. If i do it in sp_send_dbmail, i do get results with improper tabular format. So i wanted to organise them so that they can be easily viewd and understood. Please let me know if you have any questions.
Question by:parpaa
    LVL 39

    Accepted Solution

    You could wrtite your own SQL code like pseudo-code below:

    create table #test (column1 int, column2 text, column3 text, column4 text)
    insert into #test SELECT 1 ,'text2','text3','text4'
    insert into #test SELECT 2 ,'code2','code3','code4'

    DECLARE @title nvarchar(200)
    DECLARE @tableHTML nvarchar(MAX)
    SET @tableHTML =
    N'<style type="text/css">h2, body {font-family: Arial, verdana;} table{font-size:12px; border-collapse:collapse;}
    td{background-color:#F1F1F1; border:1px solid black; padding:4px;} th{background-color:#99CCFF; padding:4px;}
    h4{font-family: "Courier New", Courier, monospace; font-size: 11px;} </style>'

    SET @title = 'Summary Report - FunctionCode'
    SET @tableHTML = @tableHTML +
        N'<H1>' + @title + '</H1>' +
        N'<table border="1">' +
        N'<tr style="color:green;background-color:purple;">' +
          N'<th>Column1</th>' +
          N'<th>Column2</th>' +
          N'<th>Column3</th>' +
          N'<th>Column4</th>' +
                      td = Column1 ,'',
                      td = Column2,'',
                      td = Column3,'',
                      td = Column4,''
                FROM  #test WHERE column1 in (1,2)
                FOR XML PATH('tr'), TYPE)
           AS NVARCHAR(MAX) )
           + N'</table>' ;

    IF @tableHTML is not null
        EXEC msdb.dbo.sp_send_dbmail
                @profile_name = 'sqldba',
                @subject = @title,
                @body = @tableHTML ,
                @body_format = 'HTML' ;

    drop table #test
    LVL 25

    Expert Comment

    Nice example, Icohan.

          As Icohan used in the example, html tags are very much supported in t-sql. So, you can add them in the statement. For more information on this, you can check the following link:-


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    I wrote this interesting script that really help me find jobs or procedures when working in a huge environment. I could I have written it as a Procedure but then I would have to have it on each machine or have a link to a server-related search that …
    When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
    Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
    Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now