SQL邮件------数据库邮件
问题描述:
亲爱的, 我使用SQL Server 2008的SQL邮件------数据库邮件
我面临着一种情况,我根据查询到邮件发送到一个或多个用户。我试过这个stmt发送邮件给多个收件人
EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'imran' ,
@recipients= '[email protected];[email protected]',
@subject = 'Test mail'
它成功发送邮件。
现在我想根据查询发送邮件。对于单一员工来说,这不是问题,但如果是多个员工,我怎样才能将邮件发送给多个收件人。
这是我的问题是在这个SP发送多个收件人之一必须分开地址与;
。我如何安排收件人,以便;
介于两者之间。
谢谢。
答
使用这样的函数 -
CREATE FUNCTION coltocsv
(
--your input parameters
)
RETURNS nvarchar(3000)
AS
BEGIN
-- Declare the return variable here
declare @keywords nvarchar(3000)
--Build your csv string of keywords
Select @keywords = null
SELECT @Keywords = Coalesce(@Keywords + '; ', '') +
ColumnName
from Table
where <some condition>
--- Return the result of the function
RETURN @keywords
END
答
不幸的是,你不给我们提供了很多去 - 你不告诉我们的查询来选择你的员工,你不告诉我们什么关于Employee
表的样子,所以我们所能做的只是猜测 - 最好。
所以这里是我的猜测:你想以某种方式选择多个员工,他们的电子邮件地址应串联在一起,用分号分隔(;
),然后用于拨打sp_send_dbmail
。
一种方法做到这一点可能是这样的:
DECLARE @recipients VARCHAR(4000)
SELECT
@recipients = STUFF((SELECT ';' + EMail
FROM dbo.Employees
WHERE (some condition here to find the right employees)
FOR XML PATH('')
), 1, 1, '')
EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'imran',
@recipients,
@subject = 'Test mail'
是这些人的实际电子邮件地址?如果是,请删除或匿名它们。 – 2011-02-04 22:25:22