SQL HTML电子邮件列格式而不是行格式

时间:2019-06-19 23:33:40

标签: html sql email sql-server-2012

我试图弄清楚如何将其从表格式转换为列格式。

嗨,我知道如何通过表格格式创建SQL HTML电子邮件:即

Customer        Address     City
Joe             123 Here     Oakland 

但是我需要能够翻转它看起来像

Customer:   Joe
Address:    123 Here
City:       Oakland

我正在使用SQL 2012

任何帮助将不胜感激

请告知

DECLARE @xmlinv NVARCHAR(MAX);
DECLARE @bodyinv NVARCHAR(MAX);
DECLARE @trrowinv NVARCHAR(MAX);
DECLARE @tempemaillistinv NVARCHAR(MAX);
DECLARE @subject_anomaly NVARCHAR(MAX);
SET @subject_anomaly
    = N'Inventory Adjustments ' + CAST(@COUNTSinv AS VARCHAR(3)) + N' '
      + LEFT(CONVERT(VARCHAR(10), GETDATE(), 101), 3) + SUBSTRING(CONVERT(VARCHAR(10), GETDATE(), 101), 4, 3)
      + RIGHT(CONVERT(VARCHAR(10), GETDATE(), 101), 2);
SET @xmlinv
    = CAST(
      (
          SELECT T4.product AS 'td',
                 '',
                 T4.available AS 'td',
                 '',
                 CASE
                     WHEN T4.quarantine IS NULL THEN
                         0
                     ELSE
                         T4.quarantine
                 END AS 'td',
                 '',
                 CASE
                     WHEN T4.labels IS NULL THEN
                         0
                     ELSE
                         T4.labels
                 END AS 'td',
                 '',
                 T4.MinLog AS 'td',
                 '',
                 T.available AS 'td',
                 '',
                 CASE
                     WHEN T.quarantine IS NULL THEN
                         0
                     ELSE
                         T.quarantine
                 END AS 'td',
                 '',
                 CASE
                     WHEN T.labels IS NULL THEN
                         0
                     ELSE
                         T.labels
                 END AS 'td',
                 '',
                 T.MinLog AS 'td'
          --SELECT T4.product,T4.available,T4.quarantine,T4.labels,T4.MinLog,T.available,T.quarantine,T.labels,T.MinLog 
          FROM #t4 AS T4
              LEFT OUTER JOIN #t3 AS T3
                  ON T4.MaxRow2 = T3.MaxRow
                     AND T4.product = T3.product
              LEFT OUTER JOIN #t2 AS T2
                  ON T4.MaxRow2 = T2.MaxRow
                     AND T4.product = T2.product
              LEFT OUTER JOIN #temp AS T
                  ON T4.MaxRow2 = T.therow + 1
                     AND T4.product = T.product
          ORDER BY 1
          --ORDER BY 1,3 desc
          FOR XML PATH('tr'), ELEMENTS
      )
AS
NVARCHAR(MAX));

SET @bodyinv
    = N'<html><H2>Adjustments</H2><body bgcolor=white><table border=1  style=''border:1px;background-color:#FFFFCC;bordercolor:#FFCC00'' cellpadding=''1'' cellspacing=''0''>';
SET @trrowinv
    = N'<tr><th>Product</th><th>NewAvailable</th><th>NewQuarantine</th><th>NewLabels</th><th>LastUpdate</th><th>OldAvailable</th><th>OldQuarantine</th><th>OldLabels</th><th>PriorUpdate</th></tr>';
SET @bodyinv = @bodyinv + @trrowinv + @xmlinv + N'</table></body></html>';

SET @tempemaillistinv = N'abc@abc.com';
EXEC [msdb].[dbo].[sp_send_dbmail] @recipients = @tempemaillistinv,
                                   @body = @bodyinv,
                                   @body_format = 'HTML',
                                   @subject = @subject_anomaly,
                                   @profile_name = 'abc';

0 个答案:

没有答案