Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8459463
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T13:20:14+00:00 2026-06-10T13:20:14+00:00

I’m using something similar to example C on this MSDN page: http://msdn.microsoft.com/en-us/library/ms190307.aspx DECLARE @tableHTML

  • 0

I’m using something similar to example C on this MSDN page:
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' ;

I have a column called Rating that is set to ‘Good’ or ‘Bad’ according to my own logic.
I would like to make all lines that have a rating of ‘Bad’ have a red background. I know how to do it in HTML, but not sure how to do it with the “FOR XML” query being demonstrated in this example. Seems like I would have to add an attribute to some TD statements, and not others.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T13:20:15+00:00Added an answer on June 10, 2026 at 1:20 pm

    You can not do it directly.A little “Handcrafted” HTML is required. Here is an approach that can be used.

    Select good and bad records in separate CTE and append the “td” tags. for bad ones append the Style information as-well.

    Then append “tr” tags and combine (UNION) data rows and concatenate them using for xmlpath.

    I have removed order by columns for simplicity but you can select them in the CTE and order the results later.

    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.

    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' ;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I would like my Web page http://www.gmarks.org/math_in_e-mail.txt on my Apache 2.2.14 server to display
I'm making a simple page using Google Maps API 3. My first. One marker
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I am reading a book about Javascript and jQuery and using one of the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.