TSQL output email in html format - how to highlight first row

  • Hello,

    I have a scheduled task - running a batch file, which copies files from a shared server, configures the data and uploads into a sqlserver database.
    once a day  I need to extract the data via tsql and email the output in html format.

    I have a working script, but I want to highlight the first Row, but I cant work out how/where to code for this

    below is my script:

    declare @body varchar(max)

    --  Create the body
    set @body = cast( (
    select td = cast (Day as varchar (10)) + '</td>
    <td>' + cast(Date as varchar (12)) + '</td>
    <td>' + cast(Time as varchar (6)) + '</td>
    <td align=center>' + cast(WeekNumber as varchar (3)) + '</td>
    <td align=center>' + cast(Data_Size_MB as varchar (10)) + '</td>
    <td align=center>' + cast(Used_Data_MB as varchar (10)) + '</td>
    <td align=center>' + cast(Data_Full_Pcnt as varchar (6)) + '</td>
    <td align=center>' + cast(Log_Size_MB as varchar (10)) + '</td>
    <td align=center>' + cast(Free_Log_MB as varchar (10)) + '</td>
    <td align=center>' + cast(Log_Full_Pcnt as varchar (6))
    from (
        select    Day = datename(dw,snapshot_Date),
            Date = convert(varchar(19), Snapshot_Date, 106),
            Time = substring(CONVERT(VARCHAR, snapshot_Date, 108),0,6),
            WeekNumber = DATEPART(week, snapshot_Date),
            Data_Size_MB = Data_Size_MB,
            Used_Data_MB = Used_Data_MB,
            Data_Full_Pcnt = Data_Full_Pcnt,
            Log_Size_MB = Log_Size_MB,
            Free_Log_MB = Free_Log_MB,
            Log_Full_Pcnt = Log_Full_Pcnt,    
            snapshot_Date            
        from Ideal_Risk.dbo.Sybase_Disk_Space
        where (datepart(hh, Snapshot_Date) = 12 and datepart(mm, Snapshot_Date) >= 58)
            or (datepart(hh, Snapshot_Date) = 13 and datepart(mm, Snapshot_Date) <5)
            and database_name='IDEAL'
        ) as d
    order by snapshot_Date desc

    for xml path( 'tr' ), type ) as varchar(max) )

    set @body = '<header><font size="6" color="Green"><b><u>Sybase Database disk usage statistics</u></b></font></header>'
          + '<p>&nbsp;</p>'
          + '<table cellpadding="2" cellspacing="2" border="1">'
         + '<tr>
                    <th>Day</th>
                    <th>Date</th>
                    <th>Time</th>
                    <th>WeekNumber</th>
                    <th>Data_Size_MB</th>
                    <th>Used_Data_MB</th>
                    <th>Data_Full_Pcnt</th>
                    <th>Log_Size_MB</th>
                    <th>Free_Log_MB</th>
                    <th>Log_Full_Pcnt</th>
            </tr>'
          + replace( replace( @body, '&lt;', '<' ), '&gt;', '>' )
         + '</table>'
    print @body

    below is the current output :

    Sybase Database disk usage statistics as at

    DayDateTimeWeekNumberData_Size_MBUsed_Data_MBData_Full_PcntLog_Size_MBFree_Log_MBLog_Full_Pcnt
    Thursday23 Feb 201713:009118784.0090875.9976.511024.001015.470.83
    Wednesday22 Feb 201713:009118784.0090765.7376.411024.001008.851.48
    Tuesday21 Feb 201713:009118784.0090636.7976.301024.001001.832.17
    Monday20 Feb 201713:009118784.0090438.3376.141024.001009.731.39
    Sunday19 Feb 201713:008118784.0090299.5576.021024.001020.000.39
    Saturday18 Feb 201713:008118784.0090297.1976.021024.001019.990.39
    Friday17 Feb 201713:008118784.0090265.8475.991024.001016.240.76
    Thursday16 Feb 201713:008118784.0090133.9875.881024.001004.761.88
    Wednesday15 Feb 201713:008118784.0089984.0075.751024.001011.831.19
    Tuesday14 Feb 201713:008118784.0089825.1975.621024.001008.421.52
    Monday13 Feb 201713:008118784.0089664.7975.491024.001010.991.27
    Sunday12 Feb 201713:007118784.0090274.2476.001024.001019.930.40
    Saturday11 Feb 201713:007118784.0090272.0576.001024.001019.990.39
    Friday10 Feb 201713:007118784.0090261.5675.991024.001016.150.77
    Thursday09 Feb 201713:007118784.0090133.3875.881024.001009.401.43
    Wednesday08 Feb 201713:007118784.0089989.1475.761024.001009.381.43
    Tuesday07 Feb 201713:007118784.0089858.5975.651024.001004.091.94
    Monday06 Feb 201713:007118784.0089707.3875.521024.001006.061.75
    Sunday05 Feb 201713:006118784.0089550.2975.391024.001019.250.46
    Saturday04 Feb 201713:006118784.0095983.1280.801024.00999.762.37
    Friday03 Feb 201713:006118784.0095971.1080.791024.001014.480.93
    Thursday02 Feb 201713:006118784.0095844.6880.691024.001009.731.39
    Wednesday01 Feb 201713:006118784.0095694.8880.561024.001011.381.23
    Tuesday31 Jan 201713:006118784.0095444.9280.351024.001009.36

    also I dont understand why the last cell (or more) of the last line are missing
    and why it has stopped at 31st Jan when there is data goign back to 1st Jan 2017

  • Is that the output from the "print @body" line?  Because that will only print 4000 characters (unicode) / 8000 characters non-unicode.  There's more about the limitations of the T-SQL PRINT command on MSDN.

    As for the selective formatting of the first line - I assume you mean the first returned line of data?  There are a couple of options:

      1. change the SELECT statement that generates the data set so that it also returns a ROW_NUMBER or similar id field; put some additional code in the outer SELECT to check for that value being '1', and, if so, put in appropriate HTML formatting stuff.  (If you're on SQL 2005, this might be a bit trickier to do...)

      2. add another step to the REPLACE to look at the body text between the first <tr> / </tr> pair, again, adding formatting at this point.

    Thomas Rushton
    blog: https://thelonedba.wordpress.com

  • Thanks re number of characters.

    as for the select - I have two select statements,

    I tried adding 

    select td = Select Row_Number() Over(Order By snapshot_Date) % 1 As [TRRow]'</td>
    <td>' cast (Day as varchar (10)) + '</td>>

    but it didnt work

    I got errors

    Msg 156, Level 15, State 1, Server CIF-PROD-SQL01, Line 5 Incorrect syntax near the keyword 'Select'. Msg 102, Level 15, State 1, Server CIF-PROD-SQL01, Line 5 Incorrect syntax near ' '. Msg 156, Level 15, State 1, Server CIF-PROD-SQL01, Line 32 Incorrect syntax near the keyword 'as'.

Viewing 3 posts - 1 through 2 (of 2 total)

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