【发布时间】:2015-06-07 10:58:11
【问题描述】:
我创建了一个查询,该查询以我需要的方式提取信息和电子邮件以呈现给我们的客户。如何将其转换为由查询(查询 A)提供的存储过程?我需要它为它返回的每组独特的 cmp_code 和 cmp_e_mail 运行。因此,例如,如果 QueryA 返回以下内容,我需要为每个人单独运行电子邮件查询。
C0001 email1@asdf.com
C0002 email2@asdf.com
C0003 email3@asdf.com
查询A:
SELECT DISTINCT
[cmp_code]
,[cmp_e_mail]
FROM Table1
查询到电子邮件:
DECLARE @email nvarchar(50)
DECLARE @cmp_code nvarchar (5)
DECLARE @profile nvarchar(50)
DECLARE @subject nvarchar(100)
DECLARE @querystr nvarchar (MAX)
set @email = QueryA.[cmp_e_mail]
set @cmp_code = QueryA.[cmp_code]
set @profile = 'Reports'
set @subject = 'Test'+@cmp_code
set @querystr = 'SELECT [Year],[Week],[DueDate]
FROM Table1
WHERE [cmp_code] = '''+@cmp_code+'''';
EXEC msdb.dbo.sp_send_dbmail
@profile_name = @profile,
@recipients = @email,
@subject = @subject,
@body = 'This message is to inform you that we have not received your financial report for the following weeks.
Please remit as soon as possible and pay by the due date listed.',
@query = @querystr
【问题讨论】:
标签: tsql stored-procedures sql-server-2008-r2