try changing your call to sp_send_dbmail as follows to include the @query parameter,
EXEC msdb.dbo.sp_send_dbmail @recipients=N'ME@somewhere
Main Topics
Browse All TopicsHi. I am currently struggling with a SQL Procedure where I am attempting to send an email by a SQL Job if database Criteria is met. Currently, I have the following test syntax, which works fine.
DECLARE @AdditionalFilter BIT
DECLARE @MyDate As smalldatetime
SET @AdditionalFilter = 1
SET @MyDate = convert(varchar(10), ('07/02/2009'))
IF @AdditionalFilter = 1 AND convert(varchar(10), (getdate()), 101) = @MyDate
BEGIN
DECLARE @msg varchar(500)
SET @msg = 'To confirm that you are getting an email on a particular date time "' + '.'
EXEC msdb.dbo.sp_send_dbmail @recipients=N'ME@somewhere
SET @AdditionalFilter = 0
PRINT 'Message Sent!'
END
ELSE
BEGIN
PRINT 'No Message Sent'
END
This simply sends a message to one recipient if the date is found to be today. Simple enough, I will just put this into a SQL Job and be good.
Problem comes in that I want to insert another criteria so that if in the following SQL Statement:
SELECT Field1, Field2, Field3 from Table1 where Complete = N'No';
All values in the table that are found to have 'No' for their value (It is an nvarchar(4) field, not boolean), will be sent an email via that same job. So basically I'm asking for a Loop, but I understand that a SQL "Loop" may not be quite what I'm looking for. I've searched all around and found close to what I'm looking for but not quite enough to get me going.
I guess I'm looking for possibly a CURSOR statement, or simply something that takes the original procedure and adds values of Field1, Field2, Field3 to the email from records contained in 'Table1' where Complete = 'No'. If I'm going about anything wrong I apologize, this is still somewhat new to me.
Thank you for any assistance.
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
try changing your call to sp_send_dbmail as follows to include the @query parameter,
EXEC msdb.dbo.sp_send_dbmail @recipients=N'ME@somewhere
>> If you really don't want to use a Cursor you can create Function that sends the Mail then Call the Function as column of the select,<<
You may want to double check that. I suspect you will get the following error:
"Only functions and extended stored procedures can be executed from within a function."
Here is a loop that will send an email for every row that matches Complete = 'No'
It does not use the cool html stuff that ralmada posted, you could combine the two without much effort.
The key advantage in temp tables is if the number of rows that match the Where is significantly less than the total rows, the performance overhead in using one is not an issue. You can index the temp table as I do in the snippet, which helps. The temp table persists for the connection, when the connection ends then the index drops when the table drops.
You can add
@attach_query_result_as_fi
to the sp_send_dbmail command to put the query resultset into an attachment, otherwise it appears below the @body text, if any.
First off, thanks for all of the feedback, I know now why I use this site!!
Many answers are close, but no cigar.
From the top...
danrosenthal: Maybe in there somewhere, but I'm very sorry, I should warn you I'm a bit of an amateur... It definitely looks like it will be a cursor though, so you're on the right track for sure...
appari: for some reason, the most basic email sent with the @query parameter doesnt seem to work for me. But to answer your question, i would need separte emails for each record, sorry for not explaining better. So, yes, it would need to be a Cursor I think...
ralmada:
Thanks for the tips, in the first one, it looks close, but as I forgot to mention, it would be each record being sent individually in the email. The link looks good, it looks like this may be the path, but I'll keep it open to see if anyone else has any more specific ideas.
ONE MORE THING TO ALL...
I also forgot to mention that in this same table/View, it contains one email for each record. So for each record, the email address it is being sent to is in the same record as the Field1, Field2, Field3 columns. Not sure it that makes it easier or harder, but in case this helps im passing it along...
Again, thanks to everyone so far...
folderol:
It looks like that will probably do it, sorry, I will try to test tomorrow am and let you know first thing.
Banthor:
If they were so inclined :).
Seriously, I know im asking for quite a bit here, but I just want to have a basic setup so that I can use this for many processes that could be substantially improved. Im just starting to explore all of the possibilities that sql offers, and im pretty amazed at this point.
I appreciate all of the help, and if folderol's solution works, i'll award points tomorrow.
OK, running into a dilemma, not sure where to go with this one. If I use the @query parameter, no matter the sql statement, it gives an error msg stating that it cannot find user "My Username". I tried @attach_query_result_as_fi
i would need to see your code in more detail, but the query parameter does not support any passing of parameters to it or references to columns from the connection which calls it because it runs as a new session. That's why my snippet uses the syntax CAST(@nextid as varchar).
I tested and that worked ok for me.
OK, narrowed it mostly down now.
Can include a query, as I wasn't including the full path to the database, so it wasnt finding the table ([database].[schema].[tabl
However, it hangs if I attempt to use the following statement, with tablenames changed of course...
set @selectcmd = 'select field1, field2, field3 from #tmploop join Alerts.dbo.table1 on key1=field1 and key2=field2 where #tmploop.id = ' + cast(@nextid as varchar)
select @sentto = #tmploop.recipient from #tmploop where #tmploop.id = @nextid
Thoughts??
OK, the solution, it looks like, was semi-simple, but all I could figure was to use a Cursor and set it up like this:
DECLARE @Field1 nvarchar(70)
DECLARE @Field2 nvarchar(50)
DECLARE @Email nvarchar(250)
DECLARE cursorName CURSOR for
Select Field3, Field2, Email from Alerts.dbo.Table1 where Complete = N'No'
OPEN cursorName
FETCH NEXT FROM cursorName
INTO @Field1, @Field2, @Email
While @@FETCH_STATUS = 0
Begin
EXEC msdb.dbo.sp_send_dbmail
@recipients=@Email,
@body= @Field1,
@subject = @Field2,
@profile_name = 'MY PROFILE'
FETCH NEXT FROM cursorName
INTO @Field1, @Field2, @Email
End
folderol: You spent much time and effort in helping me, and for that I'm giving you 200 points. I think you were probably dead on, but a little advanced for what I was looking for.
danrosenthal: You gave the best link for a Cursor example, so thank you.
Thank you to everyone, very well supported!
Business Accounts
Answer for Membership
by: danrosenthalPosted on 2009-07-02 at 17:40:29ID: 24769079
If I understand correctly, it sounds like a cursor would do it for you.
e.com/Prog ramming/La nguages/ SQ L_Syntax/Q _23815322. html
The comments in this question should point you in the right direction:
http://www.experts-exchang
If you have specific questions, let me know.