• Another way of building this functionality could be to:

    1) add a BIT "email result to system admins" parameter to selected stored procedures;

    2) put a conditional path in those queries to output FOR XML AUTO, ELEMENTS, ROOT('table') and pass this to your send db email stored procedure;

    3) rewrite the send db email stored procedure to take an XML parameter containing the query results from (2);

    4) run an XSLT transform http://www.sqlservercentral.com/articles/MDS/75932/ on the output XML to turn it into a HTML table before adding to email body and sending (sample XSLT code below):

    DECLARE @xslt XML =

    '<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

    xmlns:xs="http://www.w3.org/2001/XMLSchema"

    xmlns:xd="http://www.oxygenxml.com/ns/doc/xsl"

    exclude-result-prefixes="xs xd"

    version="2.0">

    <xd:doc scope="stylesheet">

    <xd:desc>

    <xd:p><xd:b>Created on:</xd:b> 2014-03-03</xd:p>

    <xd:p><xd:b>Author:</xd:b> tavisreddick@fife.ac.uk</xd:p>

    <xd:p>Takes a SQL FOR XML AUTO, ELEMENTS, ROOT('table') query result and outputs as HTML table.</xd:p>

    </xd:desc>

    </xd:doc>

    <xsl:output method="xhtml" omit-xml-declaration="yes" />

    <xsl:template match="/">

    <html>

    <head>

    <title>HTML table from SQL query</title>

    </head>

    <body>

    <xsl:apply-templates />

    </body>

    </html>

    </xsl:template>

    <xsl:template match="table">

    <table>

    <tr>

    <xsl:for-each select="*[last()]/*">

    <th><xsl:value-of select="local-name(.)" /></th>

    </xsl:for-each>

    </tr>

    <xsl:for-each select="*">

    <tr>

    <xsl:for-each select="*">

    <td><xsl:value-of select="." /></td>

    </xsl:for-each>

    </tr>

    </xsl:for-each>

    </table>

    </xsl:template>

    </xsl:stylesheet>'

    This would be more appealing if you already had XSLT supported in your database, but at least it would keep the send email procedure simpler and more robust, accepting only data and not doing the query itself (and not having to have elevated permissions, maybe).