我在这个MSDN页面上使用了类似于示例C的东西:http://msdn.microsoft.com/en-us/library/ms190307.aspx
DECLARE @tableHTML NVARCHAR(MAX) ;
SET @tableHTML =
N'<H1>Work Order Report</H1>' +
N'<table border="1">' +
N'<tr><th>Work Order ID</th><th>Product ID</th>' +
N'<th>Name</th><th>Order Qty</th><th>Due Date</th>' +
N'<th>Expected Revenue</th></tr>' +
CAST ( ( SELECT td = wo.WorkOrderID, '',
td = p.ProductID, '',
td = p.Name, '',
td = wo.OrderQty, '',
td = wo.DueDate, '',
td = (p.ListPrice - p.StandardCost) * wo.OrderQty
FROM AdventureWorks.Production.WorkOrder as wo
JOIN AdventureWorks.Production.Product AS p
ON wo.ProductID = p.ProductID
WHERE DueDate > '2004-04-30'
AND DATEDIFF(dd, '2004-04-30', DueDate) < 2
ORDER BY DueDate ASC,
(p.ListPrice - p.StandardCost) * wo.OrderQty DESC
FOR XML PATH('tr'), TYPE
) AS NVARCHAR(MAX) ) +
N'</table>' ;
EXEC msdb.dbo.sp_send_dbmail @recipients='danw@Adventure-Works.com',
@subject = 'Work Order List',
@body = @tableHTML,
@body_format = 'HTML' ;
我有一个名为Rating的列,根据我自己的逻辑设置为"Good"或"Bad"。我想让所有评级为"坏"的行都有一个红色背景。我知道如何在HTML中实现,但不知道如何使用本例中演示的"FOR XML"查询来实现。似乎我必须为一些TD语句添加一个属性,而不是其他语句。
不能直接执行。需要一点"手工制作"的HTML。这里有一个可以使用的方法。
在单独的CTE中选择好记录和坏记录,并附加"td"标记。对于不好的,还附加样式信息。
然后附加"tr"标记并组合(UNION)数据行,然后使用for xmlpath将它们连接起来。
为了简单起见,我删除了逐列排序,但您可以在CTE中选择它们,稍后对结果进行排序。
注意:我已经测试了输出HTML,它可以工作,但我不是HTML爱好者,所以不要介意HTML标签中是否有任何错误。请随时更正。
DECLARE @tableHTML NVARCHAR(MAX)
,@Data NVARCHAR (MAX)=''
SET @tableHTML =
N'<H1>Work Order Report</H1>' +
N'<table border="1">' +
N'<tr><th>Work Order ID</th><th>Product ID</th>' +
N'<th>Name</th><th>Order Qty</th><th>Due Date</th>' +
N'<th>Expected Revenue</th></tr>'
;WITH CTE_Good AS
(
SELECT HTMLRowData= N'<td>'+STR(wo.WorkOrderID)+N'</td>'
+N'<td>'+STR(p.ProductID)+N'</td>'
+N'<td>'+p.Name+N'</td>'
+N'<td>'+STR(wo.OrderQty)+N'</td>'
+N'<td>'+CONVERT(VARCHAR(10),wo.DueDate,101)+N'</td>'
+N'<td>'+STR((p.ListPrice - p.StandardCost) * wo.OrderQty)+N'</td>'
FROM AdventureWorks.Production.WorkOrder as wo
JOIN AdventureWorks.Production.Product AS p
ON wo.ProductID = p.ProductID
WHERE DueDate > '2004-04-30'
AND DATEDIFF(dd, '2004-04-30', DueDate) < 2
--AND Rating = 'Good'
)
,CTE_Bad AS
(
SELECT HTMLRowData= N'<td><p style="color:red">'+STR(wo.WorkOrderID)+N'</p></td>'
+N'<td><p style="color:red">'+STR(p.ProductID)+N'</p></td>'
+N'<td><p style="color:red">'+p.Name+N'</p></td>'
+N'<td><p style="color:red">'+STR(wo.OrderQty)+N'</p></td>'
+N'<td><p style="color:red">'+CONVERT(VARCHAR(10),wo.DueDate,101)+N'</p></td>'
+N'<td><p style="color:red">'+STR((p.ListPrice - p.StandardCost) * wo.OrderQty)+N'</p></td>'
FROM AdventureWorks.Production.WorkOrder as wo
JOIN AdventureWorks.Production.Product AS p
ON wo.ProductID = p.ProductID
WHERE DueDate > '2004-04-30'
AND DATEDIFF(dd, '2004-04-30', DueDate) < 2
--AND Rating = 'Bad'
)
SELECT @Data=(SELECT HTMLRows
FROM (
SELECT N'<tr>'+HTMLRowData+N'</tr>' AS HTMLRows FROM CTE_Good
UNION SELECT N'<tr>'+HTMLRowData+N'</tr>' AS HTMLRows FROM CTE_Bad
) mi
FOR XML PATH(''), TYPE
).value('/', 'NVARCHAR(MAX)')
SET @tableHTML=@tableHTML+@Data+N'</table>'
--SELECT @tableHTML
EXEC msdb.dbo.sp_send_dbmail @recipients='danw@Adventure-Works.com',
@subject = 'Work Order List',
@body = @tableHTML,
@body_format = 'HTML' ;