Subquery returned more than 1 value

前端 未结 2 1847
青春惊慌失措
青春惊慌失措 2021-01-16 05:01

I need to set up a job in SQL Server 2008 to run at the first of the month sending an email to our customers. However, I do not know how to loop through the results of the s

相关标签:
2条回答
  • 2021-01-16 05:33

    The error is here where you have many rows trying to be assigned to a single variable

    SET @Recipients =(SELECT DISTINCT a.EMail
         FROM   a
        --approximately 600 email addresses 
    

    You'd need to change it to a separated list thus

    SET @Recipients = STUFF(
               (select DISTINCT ';' + CAST(a.EMail AS varchar(max))
               FROM a FOR XML PATH ('')
               )
              ,1,1, '') 
    

    Note: @Recipients will need to be varchar(max)

    0 讨论(0)
  • 2021-01-16 05:51

    the problem is @recipients = @MailRecipients. It is expecting a string (an email address) and you are giving it a recordSet.

    0 讨论(0)
提交回复
热议问题