我正试图从服务器向几个收件人发送电子邮件。
我使用以下方法查询我的数据库:
SELECT Email from payrollchecksystem
而不是使用以下内容发送电子邮件
USE msdb
GO
EXEC sp_send_dbmail @profile_name='SQL Server Alerts System',
@recipients = I DONT KNOW WHAT TO PUT HERE
@subject='Test message',
@body='This is the body of the test message.
Congrates Database Mail Received By you Successfully.'
我的问题是,当我尝试发送从payrollchecksystem表中收到的所有电子邮件时,我不知道要为@recipients提供什么?
答案 0 :(得分:5)
要将您的电子邮件分隔成以分号分隔的字符串,请使用COALESCE
:
DECLARE @EmailList varchar(MAX)
SELECT @EmailList = COALESCE(@EmailList + ';', '') + Email
FROM payrollchecksystem
WHERE Email IS NOT NULL
SELECT @EmailList
然后您可以像这样使用@EmailList:
USE msdb
GO
EXEC sp_send_dbmail @profile_name='SQL Server Alerts System',
@recipients = @EmailList,
@subject='Test message',
@body='This is the body of the test message. Congrates Database Mail Received By you Successfully.'
答案 1 :(得分:2)
http://msdn.microsoft.com/en-us/library/ms190307.aspx
@recipients 是以分号分隔的列表 发送邮件的电子邮件地址 到