SQL Server Management Studio格式查询输出到HTML表中

时间:2017-01-27 01:50:55

标签: html sql-server formatting ssms

我使用下面的代码(下面)创建表格html电子邮件输出,但我的问题是如何更改列中数据的颜色。有人能告诉我如何将s.ADDED_BY的颜色设为'td'红色。

begin

DECLARE @xml NVARCHAR(MAX)
DECLARE @body NVARCHAR(MAX)
SET @xml = CAST((SELECT s.acct_no as 'td', '', s.ADDRESS1 as 'td', '', s.CITY as 'td', '',s.U_WATERCO as 'td', '', s.ADDED_BY as 'td'
from  PLshared.dbo.client s
where (U_WATERCO = '' OR U_WATERCO LIKE '%UNKNOW%')
and s.COUNTY = 'NASSAU'
and s.inactive <> 'B'
AND s.DATE_ADDED >= CONVERT(char(10), getdate()-120, 121)
order by city
FOR XML PATH('tr'), ELEMENTS ) AS NVARCHAR(MAX))

SET @body ='<html><body><H3>Clients In Nassau County with no Water Dept</H3>
<table border = 1> 
<tr>
<th> Acct No </th> <th> Address </th> <th> City </th> <th> WaterCo </th> <th> AddedBy </th> </tr>'    


SET @body = @body + @xml +'</table></body></html>'


EXEC msdb.dbo.sp_send_dbmail
@profile_name =  'mail.pacificlawnsprinklers.com',
@body = @body,
@body_format ='HTML',
@recipients = 'MMAHONEY@pacificlawnsprinklers.com', -- replace with your email address
--@copy_recipients = 'helpdesk@pacificlawnsprinklers.com', 
@subject = 'Clients in Nassau County with No Water Dept' ;


 --@attach_query_result_as_file = 0 ;
end

1 个答案:

答案 0 :(得分:0)

BEGIN
DECLARE @body VARCHAR(MAX) = ''
DECLARE @acct_no VARCHAR(500)
DECLARE @ADDRESS1 VARCHAR(500)
DECLARE @CITY VARCHAR(500)
DECLARE @U_WATERCO VARCHAR(500)
DECLARE @ADDED_BY VARCHAR(500)

DECLARE CLIENT_CURSOR CURSOR FOR
SELECT
    s.acct_no,
    s.ADDRESS1,
    s.CITY,
    s.U_WATERCO,
    s.ADDED_BY
FROM PLshared.dbo.client s (NOLOCK)
WHERE (U_WATERCO = '' OR U_WATERCO LIKE '%UNKNOW%')
    AND s.COUNTY = 'NASSAU'
    AND s.inactive <> 'B'
    AND s.DATE_ADDED >= CONVERT(char(10), getdate()-120, 121)
ORDER BY city

OPEN CLIENT_CURSOR

FETCH NEXT FROM CLIENT_CURSOR
INTO @acct_no, @ADDRESS1, @CITY,
     @U_WATERCO, @ADDED_BY

WHILE @@FETCH_STATUS = 0 BEGIN
    SET @body +=
        '<tr>' +
            '<td>' + ISNULL(@acct_no, '') + '</td>' +
            '<td>' + ISNULL(@ADDRESS1, '') + '</td>' +
            '<td>' + ISNULL(@CITY, '') + '</td>' +
            '<td>' + ISNULL(@U_WATERCO, '') + '</td>' +
            '<td><font color="red">'+ ISNULL(@ADDED_BY, '')  +'</font></td>
         </tr>'
    FETCH NEXT FROM CLIENT_CURSOR
    INTO @acct_no, @ADDRESS1, @CITY,
     @U_WATERCO, @ADDED_BY
END
CLOSE CLIENT_CURSOR
DEALLOCATE CLIENT_CURSOR

SET @body = '<html><body><H3>Clients In Nassau County with no Water Dept</H3>
             <table border = 1> 
             <tr><th> Acct No </th> <th> Address </th> <th> City </th> <th> WaterCo </th> <th> AddedBy </th> </tr>'
             + @body +
             '</table></body></html>'

EXEC msdb.dbo.sp_send_dbmail
@profile_name =  'mail.pacificlawnsprinklers.com',
@body = @body,
@body_format ='HTML',
@recipients = 'MMAHONEY@pacificlawnsprinklers.com', -- replace with your email address
--@copy_recipients = 'helpdesk@pacificlawnsprinklers.com', 
@subject = 'Clients in Nassau County with No Water Dept' ;

 --@attach_query_result_as_file = 0 ;
END