Using SSIS to send a HTML E-Mail Message with built-in table of Counts.
Posted
by Kevin Shyr
on Geeks with Blogs
See other posts from Geeks with Blogs
or by Kevin Shyr
Published on Wed, 02 Feb 2011 21:47:05 GMT
Indexed on
2011/02/03
23:27 UTC
Read the original article
Hit count: 387
For the record, this can be just as easily done with a .NET class with a DLL call. The two major reasons for this ending up as a SSIS package are:
- There are a lot of SQL resources for maintenance, but not as many .NET developers.
- There is an existing automated process that links up SQL Jobs (more on that in the next post), and this is part of that process.
To start, this is what the SSIS looks like:
The first part of the control flow is just for the override scenario.
In the Execute SQL Task, it calls a stored procedure, which already formats the result into XML by using "FOR XML PATH('Row'), ROOT(N'FieldingCounts')". The result XML string looks like this:
<FieldingCounts>
<Row>
<CellId>M COD</CellId>
<Mailed>64</Mailed>
<ReMailed>210</ReMailed>
<TotalMail>274</TotalMail>
<EMailed>233</EMailed>
<TotalSent>297</TotalSent>
</Row>
<Row>
<CellId>M National</CellId>
<Mailed>11</Mailed>
<ReMailed>59</ReMailed>
<TotalMail>70</TotalMail>
<EMailed>90</EMailed>
<TotalSent>101</TotalSent>
</Row>
<Row>
<CellId>U COD</CellId>
<Mailed>91</Mailed>
<ReMailed>238</ReMailed>
<TotalMail>329</TotalMail>
<EMailed>291</EMailed>
<TotalSent>382</TotalSent>
</Row>
<Row>
<CellId>U National</CellId>
<Mailed>63</Mailed>
<ReMailed>286</ReMailed>
<TotalMail>349</TotalMail>
<EMailed>374</EMailed>
<TotalSent>437</TotalSent>
</Row>
</FieldingCounts>
This result is saved into an internal SSIS variable with the following settings on the General tab and the Result Set tab:
Now comes the trickier part. We need to use the XML Task to format the XML string result into an HTML table, and I used Direct input XSLT
And here is the code of XSLT:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" indent="yes"/>
<xsl:template match="/ROOT">
<table border="1" cellpadding="6">
<tr>
<td></td>
<td>Mailed</td>
<td>Re-mailed</td>
<td>Total Mail (Mailed, Re-mailed)</td>
<td>E-mailed</td>
<td>Total Sent (Mailed, E-mailed)</td>
</tr>
<xsl:for-each select="FieldingCounts/Row">
<tr>
<xsl:for-each select="./*">
<td>
<xsl:value-of select="." />
</td>
</xsl:for-each>
</tr>
</xsl:for-each>
</table>
</xsl:template>
</xsl:stylesheet>
Then a script task is used to send out an HTML email (as we are all painfully aware that SSIS Send Mail Task only sends plain text)
Note on this code: notice the email list has Replace(";", ","). This is only here because the list is configurable in the SQL Job Step at Set Values, which does not react well with colons as email separator, but system.Net.Mail only handles comma as email separator, hence the extra replace in the string.
The result is a nicely formatted email message with count information:
© Geeks with Blogs or respective owner