【问题标题】:SQL Server Management Studio format query output into an HTML tableSQL Server Management Studio 格式查询输出到 HTML 表中
【发布时间】:2017-01-27 01:50:55
【问题描述】:

我使用以下代码(如下)创建表格 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

【问题讨论】:

    标签: html sql-server formatting ssms


    【解决方案1】:
    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
    

    【讨论】:

      猜你喜欢
      • 2010-09-28
      • 2016-10-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-31
      • 2011-09-01
      • 1970-01-01
      相关资源
      最近更新 更多