Using sp_send_dbmail with recieptent and content from single sql row

前端 未结 1 1560
情书的邮戳
情书的邮戳 2021-01-23 15:32

I have a table filled with oldUserID, newUserID, name and email. I want to use sp_send_dbmail to the email on each row. For example:

oldUserID | newUserID

1条回答
  •  囚心锁ツ
    2021-01-23 15:42

    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
    

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