Using sp_send_dbmail with recieptent and content from single sql row

China☆狼群 提交于 2019-12-02 05:26:54
DECLARE
      @txt NVARCHAR(MAX)
    , @name NVARCHAR(60)
    , @email VARCHAR(100)

DECLARE cur CURSOR FAST_FORWARD READ_ONLY LOCAL FOR
    SELECT 'Your old user id: ' + CAST(oldUserID AS NVARCHAR(100)) 
         + ', your new user id: ' + CAST(newUserID AS NVARCHAR(100)), name, email
    FROM ...

OPEN cur

FETCH NEXT FROM cur INTO @txt, @name, @email

    WHILE @@fetch_status = 0
    BEGIN

        EXEC msdb.dbo.sp_send_dbmail @profile_name = ...
                                   , @recipients = @email
                                   , @subject = @name
                                   , @body = @txt
                                   , @body_format = 'HTML'

    FETCH NEXT FROM cur INTO @txt, @name, @email

    END

CLOSE cur
DEALLOCATE cur
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!