Solved

Controlling the format of individual columns in a query using sp_send_dbmail

Posted on 2011-03-22
2
1,716 Views
Last Modified: 2012-05-11
I have written a simple query to email data using sp_send_dbmail as per the example in Experts Exchange.
I use style sheets to control the format and this works fine for all columns returned using the style for td but I have a need to control the format of individual columns of data only e.g. control the text alignment/font size/background colour etc of the value column data independently of the column header.
Can this be done? If so how please.

DECLARE @tableHTML  NVARCHAR(MAX);

SET @tableHTML =
 N'
      
<head>
      <STYLE TYPE="text/css">

p.title{font-family:arial;font-size:12pt;color:blue;font:bold}
th{font-family:arial;font-size:10pt;color:white;background-color:blue;text-align:left;padding-right:10px}
td{font-family:arial;font-size:8pt;color:black;background-color:white;border:1px solid black}
</STYLE>
</head>
      <p class="title">Sales Report UK</p>
    <table>
    <tr>
    <th>Period</th>
    <th>Posting Date</th>
    <th>Salesperson</th>
    <th>Ship To Customer</th>
    <th>Item.....</th>
    <th>Description</th>
    <th>Qty</th>
    <th>Value (£)</th></tr>' +
    CAST ( ( SELECT td = Period,'',
                              td = [Posting Date],'',
                    td = Salesperson, '',
                    td = Customer, '',
                    td = Item, '',
                    td = Description, '',
                    td = Qty, '',
                    td = [Line Val (LCY)]
              FROM [$ UK Sales Analysis] AS SA
              WHERE [Product Series]='ProductA'
              ORDER BY Period,[Posting Date],Salesperson,Customer
              --GROUP BY Period
              --ORDER BY [Product Series]
             
              FOR XML PATH('tr'), TYPE
    ) AS NVARCHAR(MAX) ) +
    N'</table>
    <p class="title">Sales Report France</p>
    <table class="table">
    <tr class="header">
    <th>Period</th>
    <th>Posting Date</th>
    <th>Salesperson</th>
    <th>Ship To Customer</th>
    <th>Item</th>
    <th>Description</th>
    <th>Qty</th>
    <th>Value(€)</th></tr>' +
    CAST ( ( SELECT td = Period,'',
                              td = [Posting Date],'',
                    td = Salesperson,'',
                    td = Customer,'',
                    td = Item,'',
                    td = Description,'',
                    td = Qty,'',
                    td = [Line Val (€)]
              FROM [$ FR Sales Analysis] AS SA
              WHERE [Product Series]='ProductA'
              ORDER BY Period,[Posting Date],Salesperson,Customer
              --GROUP BY Period
              --ORDER BY [Product Series]
             
              FOR XML PATH('tr'), TYPE
    ) AS NVARCHAR(MAX) ) +
    N'</table> '

EXEC msdb.dbo.sp_send_dbmail
      @recipients='xxx@yyy.com',
    @subject = 'Sales Report',
    @body = @tableHTML,
    @body_format = 'HTML',
      @profile_name ='SQL'
0
Comment
Question by:kcoxon
2 Comments
 
LVL 4

Accepted Solution

by:
Ztinel earned 500 total points
ID: 35188492
hi, try changing portion of your code as below:
...
CAST ( ( SELECT 
                [td/@class]='anyclassname1',
                td = Period,
                "*"='',
                [td/@class]='anyclassname2',
                td = [Posting Date],
                "*"='',
                [td/@class]='anyclassname3',
                td = Salesperson,
                "*"='',
                [td/@class]='anyclassname4',
                td = Customer,
                "*"='',
                [td/@class]='anyclassname5',
                td = Item,
                "*"='',
                [td/@class]='anyclassname6',
                td = Description,
                "*"='',
                [td/@class]='anyclassname7',
                td = Qty,
                "*"='',
                [td/@class]='anyclassname8',
                td = [Line Val (LCY)]
              FROM [$ UK Sales Analysis] AS SA 
              WHERE [Product Series]='ProductA'
              ORDER BY Period,[Posting Date],Salesperson,Customer
              --GROUP BY Period 
              --ORDER BY [Product Series]
              FOR XML PATH('tr'), TYPE 
    ) AS NVARCHAR(MAX) ) +
...

Note: create a css classes for each td above.

Open in new window

0
 

Author Closing Comment

by:kcoxon
ID: 35188920
Brilliant. Thanks.
Watch out for the next question following shortly.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Long way back, we had to take help from third party tools in order to encrypt and decrypt data.  Gradually Microsoft understood the need for this feature and started to implement it by building functionality into SQL Server. Finally, with SQL 2008, …
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

760 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

18 Experts available now in Live!

Get 1:1 Help Now