我需要在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