Hi @Simflex
Please check this untested query:
CREATE PROCEDURE [dbo].[GetRegistrationInfo]
AS
BEGIN
BEGIN
DECLARE Register_Cursor CURSOR FOR
SELECT Name, COALESCE(NULLIF(PersonalEmail,''),WorkEmail) AS EmailAddress
FROM AuthorizedUsers au
WHERE NOT EXISTS (SELECT * FROM Employees e WHERE e.email IN (au.work_email, au.home_email) )
ORDER BY Name ASC
OPEN Register_Cursor
DECLARE @fullname NVARCHAR(100)
DECLARE @email NVARCHAR(MAX)
-- Get the current MAX ID
DECLARE @mailID INT
SELECT @mailID = MAX(mailid) FROM [Notifications]
-- Start reading each record from the cursor.
FETCH NEXT FROM Register_Cursor INTO @fullname, @email
WHILE @@FETCH_STATUS = 0
BEGIN
SET @mailID = @mailID + 1
INSERT INTO [Notifications] (mailid, mailContent, FullName, email_Addr, sender, IsSent)
VALUES (@mailID,
'This is a computer generated email message.
Please DO NOT use the REPLY button above to respond to this email.
Dear '+@FullName+':
Thanks for registering for the Training!
Below are details of your registration information:
Your UserName is: '+@email+'.
Your Password is: '12334r'.
Once you have retrieved your login information, please click the link below to get back to Training login screen and begin to begin to enjoy the benefits of membership.
http://servername/training/
Regards,
The Registrations & Elections Office.', @FullName, @email, 'NoReply@serverdomain', 'No')
FETCH NEXT FROM Register_Cursor INTO @FullName, @email
END
CLOSE Register_Cursor
DEALLOCATE Register_Cursor
END
BEGIN
DECLARE MAIL_CURSOR CURSOR FOR
SELECT mailid, email_Addr, sender, mailcontent
FROM [Notifications]
WHERE IsSent = 'No'
ORDER BY mailid
DECLARE @mail1 INT
DECLARE @sender NVARCHAR(100)
DECLARE @content1 NVARCHAR(4000)
DECLARE @mail_address NVARCHAR(MAX)
OPEN MAIL_CURSOR
FETCH NEXT FROM MAIL_CURSOR INTO @mail1, @mail_address, @sender, @content1
WHILE @@FETCH_STATUS = 0
BEGIN
-- exec sp_send_cdontsmail @mail1, null,null,@content1,null
EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'Elections_Office',
@recipients = @mail_address,
@subject = 'Your Account Details',
@body = @content1;
-- Update the record in Notifications table where IsSent = 'No'.
UPDATE [Notifications]
SET IsSent = 'Yes'
WHERE IsSent = 'No' AND mailid = @mail1
FETCH NEXT FROM MAIL_CURSOR INTO @mail1, @sender, @content1
END
CLOSE MAIL_CURSOR
DEALLOCATE MAIL_CURSOR
END
END
Best regards,
Cosmog Hong
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our Documentation to enable e-mail notifications if you want to receive the related email notification for this thread.