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
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