将SQL查询发送到单个接收器

时间:2017-08-26 20:45:31

标签: sql-server tsql

我有10位顾问,我希望每周五晚上发送一份电子邮件报告,报告他们每周都在报告他们(报告)。

此报告应通过电子邮件发送,其中结果显示在mailbody(未附加)

现在我只设法做了一个报告,我发送给每个人,包括每个咨询,我把每个人的电子邮件地址都放在@recipients中。但是从现在开始,我会向每个咨询发送个人报告,只有他们自己的结果。

请帮我正确的方向。这就是我到目前为止所拥有的。将他们的电子邮件地址加入查询没问题。

DECLARE @bodyMsg nvarchar(max)
DECLARE @subject nvarchar(max)
DECLARE @tableHTML nvarchar(max)

SET @subject = 'Weekly report - Consult'


SET @tableHTML = 
N'<h1 style="color: #4485b8; font-family:Arial;">Weekly report - Consult</h1>' +
N'<table style="width: 600px; font-family:Arial; float: left;" border="1" cellpadding="5">' +
N'<tr style="height: 21px; font-family:Arial;">
<th></th>
<th style="text-align:left;">Consult</th>
<th style="text-align:left;">Last</th>
<th style="text-align:right;">Time</th>
<th style="text-align:right;">Amount</th>
</tr>' +

CAST ( ( 

SELECT 
td = OrdLn.R1,'',
td = R1.Nm,'',
td = MAX(OrdLn.TrDt),'',
td = CAST(ROUND(SUM(NoInvoAb + NoInvo),2) as int),'',
td = CAST(ROUND(SUM(DInvoSF + DInvoIF),0) as int),''

FROM OrdLn
LEFT JOIN R1 ON R1.RNo = OrdLn.R1
LEFT JOIN Prod ON Prod.ProdNo = OrdLn.ProdNo
WHERE Prod.Gr3 > 0
    AND OrdLn.R1 <= '990'
    AND OrdLn.R1 > '0'
    AND OrdLn.TrDt >= CONVERT(int,CONVERT(varchar,DATEADD(wk, DATEDIFF(wk, 0, GETDATE()) - 1, 0),112))
    AND OrdLn.TrDt <= CONVERT(int,CONVERT(varchar,DATEADD(wk, DATEDIFF(wk, 6, GETDATE()), 6),112))
GROUP BY OrdLn.R1
    ,R1.Nm
ORDER BY OrdLn.R1 ASC


FOR XML PATH('tr'), TYPE 
) AS NVARCHAR(MAX) ) +
N'</table>' 

EXEC msdb.dbo.sp_send_dbmail @recipients='john.doe@domain.com',
@profile_name = 'Mikael', 
@subject = @subject,
@body = @tableHTML,
@body_format = 'HTML'

0 个答案:

没有答案