SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Loop Through the records - send email - set the value of the field - Need Code Help


Loop Through the records - send email - set the value of the field - Need Code Help

Author
Message
skb 44459
skb 44459
SSC Eights!
SSC Eights! (971 reputation)SSC Eights! (971 reputation)SSC Eights! (971 reputation)SSC Eights! (971 reputation)SSC Eights! (971 reputation)SSC Eights! (971 reputation)SSC Eights! (971 reputation)SSC Eights! (971 reputation)

Group: General Forum Members
Points: 971 Visits: 297
-- I am hoping not to use cursor

-- Query is

select * from Orders where EmailSent = 0

--I want to loop through records with above query send email and Set Emailsent field

--Email Code
EXEC msdb.dbo.sp_send_dbmail
@profile_Name ='Administrator',
@recipients= Orders.CustomerEmailAddress ,
@subject = 'Please send Feedback for Order#' + Orders.OrderNum ,
@body = 'Please send Feedback for Order#' + Orders.OrderNum

--Update Field

set Orders.EmailSent = 1
Lowell
Lowell
SSC Guru
SSC Guru (72K reputation)SSC Guru (72K reputation)SSC Guru (72K reputation)SSC Guru (72K reputation)SSC Guru (72K reputation)SSC Guru (72K reputation)SSC Guru (72K reputation)SSC Guru (72K reputation)

Group: General Forum Members
Points: 72810 Visits: 40956
here's an example of what you are asking;
you'll have to use a cursor if you want to send emails on a per-email basis from the data.

remember that when you assign parameters, you cannot concatent/use functions/manipulate anything in the assignment, that has to occur BEFORE you assign it.

--so this is bad:
EXEC msdb.dbo.sp_send_dbmail
@subject = 'Please send Feedback for Order#' + Orders.OrderNum

--this is the correct way:
declare @var varchar(128)
SET @var ='Please send Feedback for Order#' + Orders.OrderNum
EXEC msdb.dbo.sp_send_dbmail
@subject =@var



and here's the full exmaple:



DECLARE
@isql VARCHAR(2000),
@email VARCHAR(64),
@order VARCHAR(64),
@emailSubject varchar(128),
@emailBody varchar(128)
DECLARE c1 CURSOR FOR
SELECT
CustomerEmailAddress,
OrderNum
FROM Orders
WHERE EmailSent = 0

OPEN c1
FETCH NEXT FROM c1 INTO @email,@order
WHILE @@FETCH_STATUS <> -1
BEGIN
SELECT
@emailSubject = 'Please send Feedback for Order#' + @order,
@emailBody = @emailSubject --you'd build a bigger, more descriptive string for the body.

--you cannot concatenate strings as the parameters for a query, they must be eitehr a static string or an already built variable.
EXEC msdb.dbo.sp_send_dbmail
@profile_Name ='Administrator',
@recipients= @email ,
@subject = @emailSubject,
@body = @emailBody


FETCH NEXT FROM c1 INTO @email,@order
END
CLOSE c1
DEALLOCATE c1



Lowell
--help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!
skb 44459
skb 44459
SSC Eights!
SSC Eights! (971 reputation)SSC Eights! (971 reputation)SSC Eights! (971 reputation)SSC Eights! (971 reputation)SSC Eights! (971 reputation)SSC Eights! (971 reputation)SSC Eights! (971 reputation)SSC Eights! (971 reputation)

Group: General Forum Members
Points: 971 Visits: 297
Thanks for the reply.
Another Quick Question. My Email body is very long.
Ideally i would like to store the Body in a text file and read the text file into a SQL Variable like

set @EmailBody = 'read from the text file

Is it possible ?
skb 44459
skb 44459
SSC Eights!
SSC Eights! (971 reputation)SSC Eights! (971 reputation)SSC Eights! (971 reputation)SSC Eights! (971 reputation)SSC Eights! (971 reputation)SSC Eights! (971 reputation)SSC Eights! (971 reputation)SSC Eights! (971 reputation)

Group: General Forum Members
Points: 971 Visits: 297
Thanks for the reply.
Another Quick Question. My Email body is very long.
Ideally i would like to store the Body in a text file and read the text file into a SQL Variable like

set @EmailBody = 'read from the text file

Is it possible ?
skb 44459
skb 44459
SSC Eights!
SSC Eights! (971 reputation)SSC Eights! (971 reputation)SSC Eights! (971 reputation)SSC Eights! (971 reputation)SSC Eights! (971 reputation)SSC Eights! (971 reputation)SSC Eights! (971 reputation)SSC Eights! (971 reputation)

Group: General Forum Members
Points: 971 Visits: 297
Thanks for the reply.
Another Quick Question. My Email body is very long.
Ideally i would like to store the Body in a text file and read the text file into a SQL Variable like

set @EmailBody = 'read from the text file

Is it possible ?
skb 44459
skb 44459
SSC Eights!
SSC Eights! (971 reputation)SSC Eights! (971 reputation)SSC Eights! (971 reputation)SSC Eights! (971 reputation)SSC Eights! (971 reputation)SSC Eights! (971 reputation)SSC Eights! (971 reputation)SSC Eights! (971 reputation)

Group: General Forum Members
Points: 971 Visits: 297
Thanks for the reply.
Another Quick Question. My Email body is very long.
Ideally i would like to store the Body in a text file and read the text file into a SQL Variable like

set @EmailBody = 'read from the text file

Is it possible ?
skb 44459
skb 44459
SSC Eights!
SSC Eights! (971 reputation)SSC Eights! (971 reputation)SSC Eights! (971 reputation)SSC Eights! (971 reputation)SSC Eights! (971 reputation)SSC Eights! (971 reputation)SSC Eights! (971 reputation)SSC Eights! (971 reputation)

Group: General Forum Members
Points: 971 Visits: 297
Thanks for the reply.
Another Quick Question. My Email body is very long.
Ideally i would like to store the Body in a text file and read the text file into a SQL Variable like

set @EmailBody = 'read from the text file

Is it possible ?
Lowell
Lowell
SSC Guru
SSC Guru (72K reputation)SSC Guru (72K reputation)SSC Guru (72K reputation)SSC Guru (72K reputation)SSC Guru (72K reputation)SSC Guru (72K reputation)SSC Guru (72K reputation)SSC Guru (72K reputation)

Group: General Forum Members
Points: 72810 Visits: 40956
skb 44459 (10/19/2012)
Thanks for the reply.
Another Quick Question. My Email body is very long.
Ideally i would like to store the Body in a text file and read the text file into a SQL Variable like

set @EmailBody = 'read from the text file

Is it possible ?


of course;
it's just a separate select statement:

SELECT @EmailBody = EmailTemplate
FROM Campaigns WHERE templateID = 42
--assuming there are customized fields for find and replace?
SET @EmailBody = REPLACE(@EmailBody, '[firstnamePlaceholder', @FirstNameFromCursor;



Lowell
--help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!
skb 44459
skb 44459
SSC Eights!
SSC Eights! (971 reputation)SSC Eights! (971 reputation)SSC Eights! (971 reputation)SSC Eights! (971 reputation)SSC Eights! (971 reputation)SSC Eights! (971 reputation)SSC Eights! (971 reputation)SSC Eights! (971 reputation)

Group: General Forum Members
Points: 971 Visits: 297
Actually i want to read from a text file . \\server1\temp\EmailBody.txt into a variable
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (217K reputation)SSC Guru (217K reputation)SSC Guru (217K reputation)SSC Guru (217K reputation)SSC Guru (217K reputation)SSC Guru (217K reputation)SSC Guru (217K reputation)SSC Guru (217K reputation)

Group: General Forum Members
Points: 217292 Visits: 41991
skb 44459 (10/19/2012)
Actually i want to read from a text file . \\server1\temp\EmailBody.txt into a variable


Did you ever get an answer for this?

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search