2017-02-23 48 views
1

的MS圖書館網站展示瞭如何創建一個電子郵件和嵌入HTML表結果添加句子。 https://msdn.microsoft.com/en-us/library/ms190307.aspxT-SQL;如何在表之前和之後以HTML格式使用sp_send_dbmail

這很好,但你應該如何在電子郵件主體表中添加開始和結束字詞? (?它必須是明顯的,肯定)

如果初始代碼是按以下,則最佳的輸出將是: -

Hi,(carriage return) 
Here is a list.(carriage return) 
The details are below:(carriage return) 

(then table goes here) 

(carriage return) 
Thank you for looking.(carriage return) 
Kind regards(carriage return) 
(carriage return) 
From us 

表的示例代碼:

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='[email protected]', 
@subject = 'Work Order List', 
@body = @tableHTML, 
@body_format = 'HTML' ; 

回答

0

使用此而是......

DECLARE @header NVARCHAR(MAX) 
DECLARE @footer NVARCHAR(MAX) 
DECLARE @tableHTML NVARCHAR(MAX) ; 

SET @header = 'Hi, </br> Here is a list. </br> The details are below </br>' 
SET @footer = '</br> Thank you for looking. </br> Kind regards </br> From us' 

SET @tableHTML = @header + 
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>' + @footer 


    EXEC msdb.dbo.sp_send_dbmail @recipients='[email protected]', 
    @subject = 'Work Order List', 
    @body = @tableHTML, 
    @body_format = 'HTML' ; 

這是我的輸出

enter image description here

+1

謝謝@manderson。由於body_format是html,但回車沒有出現。我已經看過了這裏其他的答案,他們指出,類似的問題:( – Baz

+0

沒問題,如果解決您的問題,請標記爲回答。 – manderson

+0

不幸的是事實並非如此。 – Baz

0

@manderson 我已經得到了這個工作。這是一個我以前沒有發現的html錯誤。非常感謝

相關問題