sql server - Sending Database emails to multiple recipients while tweaking the body -


i need send emails approx, twenty recipients each day, build temp table 2 columns

email_body
email_address

there @ max 50 rows in table each day

i want loop though table , execute sp_send_email

exec msdb.dbo.sp_send_dbmail         @profile_name = 'dba',         @recipients = @email_address,         @body = @email_body,         @subject = 'test email' 

is there way without cursor?

any links example appreciated, have searched , can't find such example. sure common process.

--email campaign. declare @htmlmodel varchar(max) declare @htmlbody varchar(max) select @htmlmodel = emailbody emailcampaigns id = 1 --a different process have created awesome looking formed html our known --placeholders [firstname] [lastname] [phone] [email] might ber substituted individualization --print @htmlmodel; set @htmlbody=''    declare     @mailid int,     @id    int,     @first varchar(64),     @last  varchar(64),     @phone varchar(64),     @email varchar(64)      declare c1 cursor      --################################     select id,            isnull(firstname,'friend') firstname,             isnull(lastname,'') lastname,             isnull(phone,''),            isnull(email ,'')            --the row_number if put name in database 5 times, single email.     (           select row_number() on (partition email order email,len(firstname)desc,len(lastname)desc ) rw, *            rawcontacts           email <> '') x     rw = 1     --this stays in place until ready go live email.       , email in('lowell@somedomain.com','otherreviewer@somedomain.com')     --################################     open c1     fetch next c1 @id,@first,@last,@phone,@email     while @@fetch_status <> -1       begin         set @htmlbody = replace(@htmlmodel,'[firstname]',@first)         set @htmlbody = replace(@htmlbody,'[lastname]',  @last)         set @htmlbody = replace(@htmlbody,'[phone]',     @phone)         set @htmlbody = replace(@htmlbody,'[email]',     @email)         --exec msdb.dbo.sp_send_dbmail               @profile_name = 'database mail profile name',              @recipients=@email,              @subject = 'our non profits call volunteers',              @body = @htmlbody,              @body_format = 'html',              @mailitem_id = @mailid output     --@body_format = 'text'         insert campaignrecipients(campaign,rawcontactid,mailsentid,mailsentdate)           select 1,@id,@mailid,getdate()         fetch next c1 @id,@first,@last,@phone,@email         end     close c1     deallocate c1   go 

Comments