0

I am creating a report to be send out daily. In one of the columns I have String of all of the Item numbers associated with the Sales Order (So one sales order to possibly multiple Item numbers). The Item numbers String is separated by ','s. To be able to do this I am using a Select statement where Order numbers are same, for XML PATH('') inside of a STUFF.

After I have generated this temp table of my data I am sending an email of the data out. I am using HTML formatting in a @mailmsg setting up the table and sending it. My problem is when the email is sent the column with all of the Item numbers are all on the same line and I need them to be soft returned onto a new line (but same cell).

I have attempted to use CHAR(10) and CHAR(13) in my STUFF function to no prevail.

Any suggestions? Below is my code for my stuff into my temp table:

UPDATE @SOTable
Set PODetails = STUFF( ( 
              Select ', PONumber: ' + CAST(PONum AS VARCHAR), 
              ', POQty: ' + CAST(POQty AS VARCHAR) 
From @POTable 
Where SONo = PONo
FOR XML PATH('')
     ),1, 1, '')

Then for my mail message, after I populate the table headers do this for the data:

            CAST ((
                select
                    (convert(varchar(8), SO.SONum)) as [TD align=center], '',
                    (CONVERT(varchar(8), SO.SOItmNo)) as [TD align=center], '',
                    td = (ltrim(rtrim(SO.SODesc))), '',
                    (convert(varchar(8), SO.SONSN)) as [TD align=center], '',
                    CONVERT(varchar(8), SO.SOSRNo)as [TD align=center], '',
                    convert(varchar(3), SO.SOSRQty)as [TD align=center], '',
                    convert(varchar(3), SO.SOUOM)as [TD align=center], '',
                    CAST(convert(varchar, SO.SODate, 101) AS VARCHAR) as [TD align=center], '',
                    td = SO.PODetails
                from @SOTable SO
                order by SO.SODate
                for XML RAW('tr'), ELEMENTS
            ) as nvarchar(max))
4

1 回答 1

1

查看我关于数据库邮件和不同呼叫的博客条目。

关键是将格式设置为 HTML

@body_format = 'HTML' ;

这是一个使用 Adventure 作品创建 HTML 表格的代码片段。

如果您仍有疑问,请回信。

约翰

代码片段

-- Send with embedded html table containing query data
DECLARE @VAR_HTML NVARCHAR(MAX) ;
SET @VAR_HTML =
N'<h1>Work Order Report<h1>' +
N'<table border="1">' +
N'<tbody><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 AdventureWorks2008R2.Production.WorkOrder as wo
JOIN AdventureWorks2008R2.Production.Product AS p
ON wo.ProductID = p.ProductID
WHERE DueDate > '2006-04-30'
AND DATEDIFF(dd, '2006-04-30', DueDate) < 2
ORDER BY DueDate ASC,
(p.ListPrice - p.StandardCost) * wo.OrderQty DESC
FOR XML PATH('tr'), TYPE
) AS NVARCHAR(MAX) ) +
N'</tbody></table>' 

EXEC msdb.dbo.sp_send_dbmail
@recipients='john@craftydba.com',
@subject = 'Work Order List',
@body = @VAR_HTML,
@body_format = 'HTML' ;
于 2013-09-27T18:52:31.583 回答