Friday, April 5, 2013

Changing the font color/background color of a row dynamically while sending mails from SQL Server

Normally if we like to track our data from the DB on a daily basis, either we generate some reports using SSRS and schedule the reports to specific mail ID, or we write some queries and send the summary data as a report in a mail using sp_send_dbmail in an HTML format.

If there are some specfic values which we are tracking on a daily basis, and if we want to highlight the particular row with specific color based on some conditions, it is easy to set the color dynamically in SSRS.

To achieve the same using sp_send_dbmail , we have to write SQL queries which generate such kind of XMLs.

Consider the below example. Suppose we have a table which has data as below

place
Temperature
Sirsi
30
Bangalore
35
Hyderabad
45
Mumbai
41


We have to highlight the data for which the temperature is more than 40 degrees. We can highlight either by making it as bold/changing background color/changing font color

Dynamic Background Color Change

To dynamically change background color we need xml as given below:
<tr bgcolor = "red">
       <td>Hyderabad<td>
       <td>45</td></tr>
<tr bgcolor = "white">
        <td>Sirsi<td>
        <td>30</td></tr>

Note: This is just a sample xml

We can generate such XML using below query
SELECT
         CASE WHEN temperature > 40 THEN '#F78181' 
                    ELSE 'white'
             END AS [@bgcolor],
         td = place, '',
         td = temperature, ''
FROM test_fontcolor
FOR XML PATH('tr')



To send an email use the below format

declare @tableHTML nvarchar(max)
SET @tableHTML =
      N'<H1>Test Data</H1>' +
      N'<H4>TEST_sub_header</H4>' +
      N'<H4>TEST_header_1</H4>' +
      N'<table border="1">' +
      N'<tr><th>place</th>
      <th>Temperature</th>' +
      N'</tr>' +
      CAST ( (
        SELECT
         CASE WHEN temperature > 40 THEN '#F78181' 
                    ELSE 'white'
             END AS [@bgcolor],
         td = place, '',
         td = temperature, ''
        FROM test_fontcolor
        FOR XML PATH('tr')
      ) AS NVARCHAR(MAX) ) +
      N'</table>' ;
   
EXEC msdb.dbo.sp_send_dbmail
        --@profile_name='WP Store BI Notifications',
     @recipients='v-raheg@microsoft.com',
     @from_address = 'storenote@microsoft.com',
     @subject = 'Test Background Color',
     @body = @tableHTML,
     @body_format = 'HTML' ;

The output of would be  as below:

Test Data
TEST_sub_header
TEST_header_1
place
Temperature
Sirsi
30
Bangalore
35
Hyderabad
45
Mumbai
41




Dynamaic Font Color Change

To change the color of the font dynamically the XML should look like as below:
<tr>
   <font color = "red">
         <td> Hyderabad </td>
         <td> 45 </td>
   </font>
</tr>
<tr>
   <font color = "black">
         <td> Sirsi</td>
         <td> 30 </td>
   </font>
</tr>


SQL query to generate XML as above would look like :

SELECT
      CASE WHEN Temperature>40 THEN 'red'
            ELSE 'black'
      END  AS "font/@color",
      place  as "font/td",'',
      CASE WHEN Temperature>40 THEN 'red'
            ELSE 'black'
      END  AS "font/@color",
    Temperature as "font/td"
FROM
      test_fontcolor
ORDER BY place
FOR XML PATH('tr')

NOTE: Above query does not generate XML exactly like above. For every td tag there would be one font tag. It will generate like:
<tr>
   <font color = "red">
         <td> Hyderabad </td>
   </font>
  <font color = "red">
         <td> 45 </td>
   </font>
</tr>

To send an email use the below code:

declare @tableHTML nvarchar(max)
SET @tableHTML =
                N'<H1>Test Data</H1>' +
                N'<H4>TEST_sub_header</H4>' +
                N'<H4>TEST_header_1</H4>' +
                N'<table border="1">' +
                N'<tr><th>place</th><th>Temperature</th>' +
                N'</tr>' +
                CAST ( ( SELECT
                                          CASE WHEN Temperature>40 THEN 'red'
                                                ELSE 'black'
                                          END  AS "font/@color",
                                          place  as "font/td",'',
                                          CASE WHEN Temperature>40 THEN 'red'
                                                ELSE 'black'
                                          END  AS "font/@color",
                                          Temperature as "font/td"
                                    FROM
                                          test_fontcolor
                                    ORDER BY place
                                    FOR XML PATH('tr')
                ) AS NVARCHAR(MAX) ) +
                N'</table>' ;  
                               
EXEC msdb.dbo.sp_send_dbmail
     --@profile_name='WP Store BI Notifications',
       @recipients='v-raheg@microsoft.com',
      @from_address = 'storenote@microsoft.com',
       @subject = 'Test Font Color',
       @body = @tableHTML,
       @body_format = 'HTML' ;

The out put would be :

Test Data
TEST_sub_header
TEST_header_1
place
Temperature
Sirsi
30
Bangalore
35
Hyderabad
45
Mumbai
41


Thursday, April 4, 2013

Hyper link while sending email

While sending an email from SQL server,  there is a possibility of client asking a common message at the bottom like :
"For any Feedback/Issue please contact Mr.XYZ"

When Mr.XYZ is clicked, an outlook with appropriate mail ID should open. It will be user friendly if even the subject is also automated within the hyperlink.
We can achieve this using below

<tr>For questions for feedback please mailto: <a href="mailto:raksha.r.hegde@gmail.com?subject=FeedBack">Mr.XYZ</a> </tr>

Wednesday, April 3, 2013

Sending an Email with High Importance

Normally an Email iin SQL Server using the stored proc "msdb.dbo.sp_send_dbmail"  .

A geneneric Format of sending an email using the above procedure is

  EXEC msdb.dbo.sp_send_dbmail     
                @profile_name   = 'Test SQL Mail', 
                @subject        = 'Test SQL Mail, 
                @recipients     = @ToList,
                @copy_recipients = @CCList,
                @from_address   = 'raksha.r.hegde@gmail.com
                @body_format    = 'HTML', 
                @file_attachments =@FilePath,
                @body           = @body; 
     end


To send an SQL Mail with high importance one more parameter, @importance has to be added as below:

One of the below 3 values can be set:
High
Low
Normal

Default Value is Normal.

To send an email as High Priority Email, we can use the below format

  EXEC msdb.dbo.sp_send_dbmail     
                @profile_name   = 'Test SQL Mail',
                @subject        =  'Test SQL Mail,
                @importance  = 'High',                @recipients     = @ToList,
                @copy_recipients = @CCList,
                @from_address   = 'raksha.r.hegde@gmail.com,
                @body_format    = 'HTML', 
                @file_attachments =@FilePath,
                @body           = @body;