我需要在SQL Server 2008中设置一个作业,以便在本月的第一个月向我们的客户发送电子邮件。但是,我不知道如何遍历子查询的结果。子查询返回的值超过1个
造成这种错误消息:
子查询返回大于1倍的值。 当 子查询遵循=,!=,<,< =,>,> = 或当子查询用作 表达式时,这是不允许的。
下面是相关代码:
SET @Recipients =(SELECT DISTINCT a.EMail
FROM a
--approximately 600 email addresses
SET @MailSubject = 'Customer News'
SET @MailRecipients = @Recipients
SET @MailMessage = 'Dear customer, Attached is your customer news letter.'
SET @FileName = N'E:file\to\be\attached.doc'
EXEC msdb.dbo.sp_send_dbmail @recipients = @MailRecipients,
@body = @MailMessage,
@blind_copy_recipients='[email protected]',
@subject = @MailSubject,
@file_attachments = @FileName
这个*可能是一个考虑使用很少选择一个CURSOR的地方(如果操作变得势在必行并且不再基于集合)。但是,SQL Server也支持表变量[这里讨论与CURSOR](http://www.codeproject.com/KB/database/TableVariablesVSCursor.aspx)。一个“INSERTS INTO”表变量。什么是'sp_send_dbmail'程序的*签名*? (这将导致更好的答案,而不仅仅是“为什么它不起作用”;-) – 2011-05-24 20:09:44