2

I have a query which I need to send using mail in html table format. I don't know how to structure exactly in the html table. Below is the query I am using

  WITH app AS
 (

  SELECT TOP 25 [Version].[VerID],[Model].[Name],[Version].[CreateDate],[Version].[LastDate],
  ROW_NUMBER() OVER (ORDER BY [Version].[VerID]) AS VerNumber

    FROM [DBReport].[SerialReports].[dbo].[Version] 
     full join [DBReport].[SerialReports].[dbo].[Model]

  on (Model.VerID = Version.VerID) where [Version].[TargetID]='157' and (Name like '%UI%') and [Version].LastDate between 
   dateadd(mm,-1,getdate()) and getdate()
 )

 select VerNumber,VerID,Name,CreateDate,LastDate  from app

and the format I am following is from MSDN but cant get enough idea. Can anyone give me idea How its structured if using CTE

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' ;

Thanks in advance

Simple_Noob
  • 51
  • 1
  • 3

0 Answers0