我正在尝试发送一封包含两个单独表格的电子邮件 我怎么做?
根据某人的要求添加样本数据。
这就是电子邮件应该是什么样子
产品
Item Price
Apples 1.25
Oranges 2.24
Banana 0.29
销售
Month Item Sold
Feb Apples $5.00
Feb Oranges $10.00
这是代码。我必须注释掉if语句并删除一个开头,以便编译没有错误。
Declare @nvMessage nvarchar(1000)
Declare @nvSubject nvarchar(1000)
Declare @nvQuery nvarchar(4000)
DECLARE @tabdata nvarchar(max)
DECLARE @table nvarchar(max)
DECLARE @tableHTML nvarchar(max)
Declare @iWidth int
Set @iWidth = 4000
Set @nvSubject = 'Month End Report'
Declare @MonthBegin datetime
Declare @MonthEnd datetime
Set @MonthBegin = DATEADD(month, DATEDIFF(month, -1, getdate()) - 2, 0)
Set @MonthEnd = DATEADD(ss, -1, DATEADD(month, DATEDIFF(month, 0, getdate()), 0))
------------------------------------------------------------------------------------------------------------------------
SET @tableHTML =
N'<h2><font face="Calibri">Report: ' + '</font></h2>' +
N'<table border="1" rules="none" cellpadding="6" cellspacing="-1"><font face="Calibri" size=2>' +
N'<tr>' +
N'<th bgcolor="#C5D9F1">Table Field you want to display</th>' +
N'<th bgcolor="#C5D9F1"> Another Table Field you want to display</th>' +
N'</tr>' +
CAST
(
(
SELECT * FROM ITEMS
FOR XML PATH('tr'), TYPE
) AS NVARCHAR(MAX)
) +'</table>' + '<hr color = "black"> </hr>'
--IF
-- (
-- SELECT
-- COUNT(*)
-- FROM
-- table2
-- ) > 0
-- if there is data in the second table then show it. If not dont.
SET @tableHTML = @tableHTML +
N'<h2><font face="Calibri">Report: ' + '</font></h2>' +
N'<table border="1" rules="none" cellpadding="6" cellspacing="-1"><font face="Calibri" size=2>' +
N'<tr>' +
N'<th bgcolor="#C5D9F1">Table Field you want to display</th>' +
N'<th bgcolor="#C5D9F1"> Another Table Field you want to display</th>' +
N'</tr>' +
CAST
(
(
SELECT * FROM SALES
FOR XML PATH('tr'), TYPE
) AS NVARCHAR(MAX)
) +'</table>' + '<hr color = "black"> </hr>'
-- then send out your mail
BEGIN
SET @nvMessage = 'Month End Report'
EXEC msdb.dbo.sp_send_dbmail_mod
@recipients = @nvRecipients,
@subject = @nvSubject,
@body = @tableHTML,
@body_format = 'HTML'
END
现在我有太多代码,它要我添加更多文字。 现在我有太多代码,它希望我添加更多文本。 现在我有太多代码,它希望我添加更多文本。 现在我有太多代码,它希望我添加更多文本。
答案 0 :(得分:2)
I usually use an HTML table to do this. This will create an email with multiple tables.
DECLARE @NewLineChar AS CHAR(2) = CHAR(13) + CHAR(10);
If
(
SELECT
COUNT(*)
FROM
#TABLE_YOU_WANT_DATA_FROM
) = 0
BEGIN
SET @tableHTML = @NewLineChar
END
SET @tableHTML =
N'<h2><font face="Calibri">Report: ' + '</font></h2>' +
N'<table border="1" rules="none" cellpadding="6" cellspacing="-1"><font face="Calibri" size=2>' +
N'<tr>' +
N'<th bgcolor="#C5D9F1">field1</th>' +
N'<th bgcolor="#C5D9F1">field2</th>' +
N'</tr>' +
CAST
(
(
SELECT
td = x1,'' AS [field1],
td = x2 ,'' AS [field2]
FROM
TABLE_YOU_WANT_DATA_FROM
FOR XML PATH('tr'), TYPE
) AS NVARCHAR(MAX)
) +'</table>' + '<hr color = "black"> </hr>'
END
IF
(
SELECT
COUNT(*)
FROM
table2
) > 0
-- if there is data in the second table then show it. If not dont.
BEGIN
SET @tableHTML = @tableHTML +
N'<h2><font face="Calibri">Report: ' + '</font></h2>' +
N'<table border="1" rules="none" cellpadding="6" cellspacing="-1"><font face="Calibri" size=2>' +
N'<tr>' +
N'<th bgcolor="#C5D9F1">field1</th>' +
N'<th bgcolor="#C5D9F1">field2</th>' +
N'</tr>' +
CAST
(
(
SELECT
td = x1,'' AS [field1],
td = x2 ,'' AS [field2]
FROM
TABLE_YOU_WANT_DATA_FROM
FOR XML PATH('tr'), TYPE
) AS NVARCHAR(MAX)
) +'</table>' + '<hr color = "black"> </hr>'
END
-- then send out your mail
BEGIN
SET @nvMessage = 'Month End Report'
EXEC msdb.dbo.sp_send_dbmail_mod
@recipients = @nvRecipients,
@subject = @nvSubject,
@body = @tableHTML,
@body_format = 'HTML'
END
答案 1 :(得分:0)
解决方案不佳,但这与使用send_dbmail的情况一样接近。它仅在两个表中具有相同列时才有效:
SET @nvquery =&#39; SELECT * FROM#TME1 UNION ALL SELECT * FROM#TME1 WHERE 1 = 2 UNION ALL SELECT * FROM#TME2&#39;