Print Procedure Parameter

  • Hi

    I have an issue printing code from a procedure. When passing the parameter to sp_send_dbmail, I receive an email with all the information contained in the body text. See below code. I have a database mail account and profile that is the same as my servername

    DECLARE @TABLEHTMLNVARCHAR(MAX),

    @EMAILSUBJECTNVARCHAR(100),

    @serverNVARCHAR(50),

    @HEADERNVARCHAR(200)

    SELECT @EMAILSUBJECT= @server + ' - Server Daily Health Checks'

    SELECT @HEADER= CONVERT(NVARCHAR(30),GETDATE(),120) +'

    '

    --*************************** DATABASES

    SELECT @TABLEHTML = '</table>' +

    '<font face="Verdana" size="4">

    Databases</font>

    <table id="AutoNumber1" border="1" cellspacing="0" cellpadding="2" width="100%" id="AutoNumber1" style="BORDER-COLLAPSE: collapse" borderColor="#111111">

    <tr>

    <td bgColor="#000080"><font face="Verdana" size="1" color="#FFFFFF"><strong>Database Name</strong></font></td>

    <td bgColor="#000080"><font face="Verdana" size="1" color="#FFFFFF"><strong>Created By</strong></font></td>

    <td bgColor="#000080"><font face="Verdana" size="1" color="#FFFFFF"><strong>Created Date</strong></font></td>

    <td bgColor="#000080"><font face="Verdana" size="1" color="#FFFFFF"><strong>DB Size (Gb)</strong></font></td>

    <td bgColor="#000080"><font face="Verdana" size="1" color="#FFFFFF"><strong>State</strong></font></td>

    <td bgColor="#000080"><font face="Verdana" size="1" color="#FFFFFF"><strong>Recovery Model</strong></font></td>

    </tr>'

    SELECT @TABLEHTML = @TABLEHTML +

    CASE WHEN create_date > GETDATE()-1 THEN + '<td><font face="Verdana" size="1">'+ ISNULL('&RED ' + name, '') +'</font></td>' ELSE '<td><font face="Verdana" size="1">' + ISNULL('&GREEN ' + name, '') +'</font></td>' END +

    '<td><font face="Verdana" size="1">' + SUSER_SNAME(owner_sid) +'</font></td>'+

    '<td><font face="Verdana" size="1">' + CONVERT(VARCHAR(25),create_date,120) +'</font></td>' +

    '<td><font face="Verdana" size="1">' + ISNULL(CONVERT(VARCHAR(10), AA.[Total SIZE GB]), '') +'</font></td>'+

    '<td><font face="Verdana" size="1">' + ISNULL(state_desc, '') +'</font></td>'+

    '<td><font face="Verdana" size="1">' + ISNULL(recovery_model_desc, '') +'</font></td></tr>'

    FROM sys.databases MST

    INNER JOIN (SELECT b.name [LOG_DBNAME], CONVERT(DECIMAL(22,2),SUM(CONVERT(DECIMAL(22,2),(a.size * 8)) /1024)/1024) [Total SIZE GB]

    FROM sys.sysaltfiles A

    INNER JOIN sys.databases B ON A.dbid = B.database_id

    GROUP BY b.name) AA ON AA.[LOG_DBNAME] = MST.name

    ORDER BY MST.name ASC

    SELECT @TABLEHTML = @TABLEHTML + '</table>'

    --PRINT @TABLEHTML

    EXEC msdb.dbo.sp_send_dbmail

    @profile_name= @@SERVERNAME,

    @recipients= 'email@address.com',

    @subject= @EMAILSUBJECT,

    @body= @TABLEHTML,

    @body_format= 'HTML' ;

    The reason for this is to serve as some sort of health check. An example of the results are shown below, which turns out to be a nicely formatted HTML email containing this information.

    </table><font face="Verdana" size="4">

    Databases</font>

    <table id="AutoNumber1" border="1" cellspacing="0" cellpadding="2" width="100%" id="AutoNumber1" style="BORDER-COLLAPSE: collapse" borderColor="#111111">

    <tr>

    <td bgColor="#000080"><font face="Verdana" size="1" color="#FFFFFF"><strong>Database Name</strong></font></td>

    <td bgColor="#000080"><font face="Verdana" size="1" color="#FFFFFF"><strong>Created By</strong></font></td>

    <td bgColor="#000080"><font face="Verdana" size="1" color="#FFFFFF"><strong>Created Date</strong></font></td>

    <td bgColor="#000080"><font face="Verdana" size="1" color="#FFFFFF"><strong>DB Size (Gb)</strong></font></td>

    <td bgColor="#000080"><font face="Verdana" size="1" color="#FFFFFF"><strong>State</strong></font></td>

    <td bgColor="#000080"><font face="Verdana" size="1" color="#FFFFFF"><strong>Recovery Model</strong></font></td>

    </tr><td><font face="Verdana" size="1">&GREEN master</font></td><td><font face="Verdana" size="1">sa</font></td><td><font face="Verdana" size="1">2003-04-08 09:13:36</font></td><td><font face="Verdana" size="1">0.00</font></td><td><font face="Verdana" size="1">ONLINE</font></td><td><font face="Verdana" size="1">SIMPLE</font></td></tr><td><font face="Verdana" size="1">&GREEN model</font></td><td><font face="Verdana" size="1">sa</font></td><td><font face="Verdana" size="1">2003-04-08 09:13:36</font></td><td><font face="Verdana" size="1">0.00</font></td><td><font face="Verdana" size="1">ONLINE</font></td><td><font face="Verdana" size="1">FULL</font></td></tr><td><font face="Verdana" size="1">&GREEN msdb</font></td><td><font face="Verdana" size="1">sa</font></td><td><font face="Verdana" size="1">2010-04-02 17:35:08</font></td><td><font face="Verdana" size="1">0.01</font></td><td><font face="Verdana" size="1">ONLINE</font></td><td><font face="Verdana" size="1">SIMPLE</font></td></tr><td><font face="Verdana" size="1">&RED Nortwind</font></td><td><font face="Verdana" size="1">ZA\Hermann.Lotter</font></td><td><font face="Verdana" size="1">2010-07-02 11:45:33</font></td><td><font face="Verdana" size="1">0.00</font></td><td><font face="Verdana" size="1">ONLINE</font></td><td><font face="Verdana" size="1">FULL</font></td></tr><td><font face="Verdana" size="1">&RED tempdb</font></td><td><font face="Verdana" size="1">sa</font></td><td><font face="Verdana" size="1">2010-07-01 16:05:27</font></td><td><font face="Verdana" size="1">0.01</font></td><td><font face="Verdana" size="1">ONLINE</font></td><td><font face="Verdana" size="1">SIMPLE</font></td></tr><td><font face="Verdana" size="1">&RED Test</font></td><td><font face="Verdana" size="1">ZAame.surname</font></td><td><font face="Verdana" size="1">2010-07-02 11:45:15</font></td><td><font face="Verdana" size="1">0.00</font></td><td><font face="Verdana" size="1">ONLINE</font></td><td><font face="Verdana" size="1">FULL</font></td></tr></table>

    The email part works fine no matter how many databases are attached to the server you run this on, but when I print the results (PRINT @TABLEHTML), SSMS truncates most of the code especially when it gets more than a certain amount.

    Is this a variable that can be set? Or alternatively can I pass this parameter somehow to VBScript? (I unfortunately need to pass this to VBScript) Any ideas community? Thanks in advance for any suggestions.

  • have u tried using the

    SELECT @TABLEHTML

    Replace the mail code withe the above statement and try executing the procedure

    and check what u get

    Prashant Bhatt
    Sr Engineer - Application Programming

  • Hi Prashant

    No idea why I did not think of that but it works fine. Thanks so much, I kinda feel like kicking myself for not thinking of something so simple.

    Ciao

  • but when I print the results (PRINT @TABLEHTML), SSMS truncates most of the code especially when it gets more than a certain amount.

    this is a setting in SSMS; by default the longest string you can print is 256 chars until you change it:

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • and that is also what i was looking for. thanks so much all!

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply