且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用电子邮件正文中的多个表从SQL Server数据库发送电子邮件

更新时间:2022-12-03 10:15:51

使用函数我在这里提供你可以这样做:

Using the function I provide here you can do this:

DECLARE @tbl TABLE(Field1 INT, Field2 VARCHAR(10), Field3 VARCHAR(10));
INSERT INTO @tbl VALUES
 (1,'AA','Value1')
,(2,'BB','Value1')
,(3,'CC','Value1')
,(1,'OO','Value2')
,(2,'XX','Value2')
,(3,'VV','Value2')
,(1,'qwqw','Value3')
,(2,'GGGG','Value3')
,(3,'COCO','Value3');

- 查询会将其构建为一个大的 XHTML

--The query will build this as one big XHTML

SELECT (SELECT N'Section: Field3="Value1"' AS p FOR XML PATH(''),TYPE)
,dbo.CreateHTMLTable
        (
        (SELECT * FROM @tbl WHERE Field3='Value1' FOR XML PATH('row'), ELEMENTS XSINIL)
        ,NULL,NULL,NULL
        )
,(SELECT N'Section: Field3="Value2"' AS p FOR XML PATH(''),TYPE)
,dbo.CreateHTMLTable
        (
        (SELECT * FROM @tbl WHERE Field3='Value2' FOR XML PATH('row'), ELEMENTS XSINIL)
        ,NULL,NULL,NULL
        )
,(SELECT N'Section: Field3="Value3"' AS p FOR XML PATH(''),TYPE)
,dbo.CreateHTMLTable
        (
        (SELECT * FROM @tbl WHERE Field3='Value3' FOR XML PATH('row'), ELEMENTS XSINIL)
        ,NULL,NULL,NULL
        )  
FOR XML PATH('body'),ROOT('html');

这是结果(点击运行代码段查看结果格式)

This is the result (click run code snippet to see the result formatted)

<html>
  <body>
    <p>Section: Field3="Value1"</p>
    <table>
      <thead>
        <tr>
          <th>Field1</th>
          <th>Field2</th>
          <th>Field3</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td>1</td>
          <td>AA</td>
          <td>Value1</td>
        </tr>
        <tr>
          <td>2</td>
          <td>BB</td>
          <td>Value1</td>
        </tr>
        <tr>
          <td>3</td>
          <td>CC</td>
          <td>Value1</td>
        </tr>
      </tbody>
    </table>
    <p>Section: Field3="Value2"</p>
    <table>
      <thead>
        <tr>
          <th>Field1</th>
          <th>Field2</th>
          <th>Field3</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td>1</td>
          <td>OO</td>
          <td>Value2</td>
        </tr>
        <tr>
          <td>2</td>
          <td>XX</td>
          <td>Value2</td>
        </tr>
        <tr>
          <td>3</td>
          <td>VV</td>
          <td>Value2</td>
        </tr>
      </tbody>
    </table>
    <p>Section: Field3="Value3"</p>
    <table>
      <thead>
        <tr>
          <th>Field1</th>
          <th>Field2</th>
          <th>Field3</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td>1</td>
          <td>qwqw</td>
          <td>Value3</td>
        </tr>
        <tr>
          <td>2</td>
          <td>GGGG</td>
          <td>Value3</td>
        </tr>
        <tr>
          <td>3</td>
          <td>COCO</td>
          <td>Value3</td>
        </tr>
      </tbody>
    </table>
  </body>
</html>

嵌入< style> 添加CSS格式的节点

Embedd a <style> node to add CSS formatting

在上面提供的链接中查找更多可能性和背景...

Find further possibilities and background at the link provided above...