Subquery returned more than 1 value

前端 未结 2 1849
青春惊慌失措
青春惊慌失措 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)

提交回复
热议问题