Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can not do it directly.A little "Handcrafted" HTML is required. Here is an approach that can be used. </p> <p>Select good and bad records in separate CTE and append the "td" tags. for bad ones append the Style information as-well.</p> <p>Then append "tr" tags and combine (UNION) data rows and concatenate them using for xmlpath.</p> <p>I have removed order by columns for simplicity but you can select them in the CTE and order the results later.</p> <p>Note: I have tested the output HTML and it works but I am not HTML guy so don't mind if there is any mistake in HTML tags.feel free to correct it.</p> <pre><code>DECLARE @tableHTML NVARCHAR(MAX) ,@Data NVARCHAR (MAX)='' SET @tableHTML = N'&lt;H1&gt;Work Order Report&lt;/H1&gt;' + N'&lt;table border="1"&gt;' + N'&lt;tr&gt;&lt;th&gt;Work Order ID&lt;/th&gt;&lt;th&gt;Product ID&lt;/th&gt;' + N'&lt;th&gt;Name&lt;/th&gt;&lt;th&gt;Order Qty&lt;/th&gt;&lt;th&gt;Due Date&lt;/th&gt;' + N'&lt;th&gt;Expected Revenue&lt;/th&gt;&lt;/tr&gt;' ;WITH CTE_Good AS ( SELECT HTMLRowData= N'&lt;td&gt;'+STR(wo.WorkOrderID)+N'&lt;/td&gt;' +N'&lt;td&gt;'+STR(p.ProductID)+N'&lt;/td&gt;' +N'&lt;td&gt;'+p.Name+N'&lt;/td&gt;' +N'&lt;td&gt;'+STR(wo.OrderQty)+N'&lt;/td&gt;' +N'&lt;td&gt;'+CONVERT(VARCHAR(10),wo.DueDate,101)+N'&lt;/td&gt;' +N'&lt;td&gt;'+STR((p.ListPrice - p.StandardCost) * wo.OrderQty)+N'&lt;/td&gt;' FROM AdventureWorks.Production.WorkOrder as wo JOIN AdventureWorks.Production.Product AS p ON wo.ProductID = p.ProductID WHERE DueDate &gt; '2004-04-30' AND DATEDIFF(dd, '2004-04-30', DueDate) &lt; 2 --AND Rating = 'Good' ) ,CTE_Bad AS ( SELECT HTMLRowData= N'&lt;td&gt;&lt;p style="color:red"&gt;'+STR(wo.WorkOrderID)+N'&lt;/p&gt;&lt;/td&gt;' +N'&lt;td&gt;&lt;p style="color:red"&gt;'+STR(p.ProductID)+N'&lt;/p&gt;&lt;/td&gt;' +N'&lt;td&gt;&lt;p style="color:red"&gt;'+p.Name+N'&lt;/p&gt;&lt;/td&gt;' +N'&lt;td&gt;&lt;p style="color:red"&gt;'+STR(wo.OrderQty)+N'&lt;/p&gt;&lt;/td&gt;' +N'&lt;td&gt;&lt;p style="color:red"&gt;'+CONVERT(VARCHAR(10),wo.DueDate,101)+N'&lt;/p&gt;&lt;/td&gt;' +N'&lt;td&gt;&lt;p style="color:red"&gt;'+STR((p.ListPrice - p.StandardCost) * wo.OrderQty)+N'&lt;/p&gt;&lt;/td&gt;' FROM AdventureWorks.Production.WorkOrder as wo JOIN AdventureWorks.Production.Product AS p ON wo.ProductID = p.ProductID WHERE DueDate &gt; '2004-04-30' AND DATEDIFF(dd, '2004-04-30', DueDate) &lt; 2 --AND Rating = 'Bad' ) SELECT @Data=(SELECT HTMLRows FROM ( SELECT N'&lt;tr&gt;'+HTMLRowData+N'&lt;/tr&gt;' AS HTMLRows FROM CTE_Good UNION SELECT N'&lt;tr&gt;'+HTMLRowData+N'&lt;/tr&gt;' AS HTMLRows FROM CTE_Bad ) mi FOR XML PATH(''), TYPE ).value('/', 'NVARCHAR(MAX)') SET @tableHTML=@tableHTML+@Data+N'&lt;/table&gt;' --SELECT @tableHTML EXEC msdb.dbo.sp_send_dbmail @recipients='danw@Adventure-Works.com', @subject = 'Work Order List', @body = @tableHTML, @body_format = 'HTML' ; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload