2015-02-24 74 views
0

我想从我的表使用sp_send_dbmail发送多个电子邮件,但我在运行存储过程时出现此错误。这里是我得到的错误:使用send发送多个电子邮件dbmail存储过程在sql

Parameter @attach_query_result_as_file cannot be 1 (true) when no value is specified for parameter @query. A query must be specified to attach the results of the query. 

这里是我的代码

SET ANSI_NULLS ON 
    GO 
    SET QUOTED_IDENTIFIER ON 
    GO 
    ALTER proc [dbo].[myProc] as 
    declare rscursor cursor read_only 
    for 
    select Email, FullName from myTable 
     where userActive =1 

     declare @Emails   nvarchar (100) 
     declare @FullName nvarchar (100) 


    open rscursor 
    fetch next from rscursor into @Emails, @FullName 

    while @@fetch_status=0 
     begin 

      EXEC msdb.dbo.sp_send_dbmail 
      @recipients = @Emails, 
      @subject = 'Sleep Diary Reminder', 

      @body = 'this is just test', 
      @profile_name = 'myProfile', 


      @attach_query_result_as_file = 1   

     fetch next from rscursor into @Emails, @FullName 

    end 
    close rscursor 
    deallocate rscursor 

运行我的SP

EXEC dbo.myProc 

回答

2

既然你不使用@query附加查询到邮件程序sp_send_dbmail的变量或者设置@attach_query_result_as_file = 0或者完全删除变量。或者附加查询,如果这是你想要做的 - 你可以尝试添加@query = 'SELECT GETDATE()'来查看它的工作原理。

相关问题