?
Solved

sp_send_dbmail with query specific to each recipient

Posted on 2010-08-26
6
Medium Priority
?
356 Views
Last Modified: 2012-05-10
I have the script working to send an email to each recipient. I need to include data that is specific to each recipient though.

How do I do this? I'd like to include it in the body or subject or anywhere at this point. It's not a lot of data, just one field in the table that is related to that person.

The 'REF' in the script below is the field.

*******************
declare Maillist cursor
for select EMAIL FROM dbo.test
      OPEN Maillist    
FETCH FROM Maillist into @emailAddress

WHILE NOT (@@fetch_status=-1)    
 BEGIN  
   begin
set nocount on
USE [Goldmine] SELECT EMAIL, REF FROM  dbo.test
if @@ROWCOUNT >0
begin
EXEC msdb.dbo.sp_send_dbmail
*************
0
Comment
Question by:philthompson1
  • 3
  • 2
6 Comments
 
LVL 8

Expert Comment

by:Bob Hoffman
ID: 33531538
sp_send_dbmail accepts all kinds of parms that will allow you to customize the email.

http://msdn.microsoft.com/en-us/library/ms190307.aspx
0
 
LVL 9

Expert Comment

by:Sander Stad
ID: 33531584
You can add the value in several places in the e-mail. Use this page to get the right parameter for sp_db_sendmail  http://msdn.microsoft.com/en-us/library/ms190307.aspx
0
 

Author Comment

by:philthompson1
ID: 33531608
Yes, I've seen that many times. I'm not sure how to show the specific data per recipient. There might be up to 10 emails each having a different Quote.

Example:

Dear Mr. Jones,
Your "quote # 12345" is about to expire in 15 days.

Thank you,
XXXX

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 8

Accepted Solution

by:
Bob Hoffman earned 2000 total points
ID: 33531680
open a cursor on "SELECT EMAIL, REF FROM  dbo.test" loop through executing sp_send_dbmail loading the parms from each read until all the emails are sent
0
 

Author Comment

by:philthompson1
ID: 33532622
I did get it to work. All I had to do is declare and  add the @quoteref column after the @emailaddress

--FETCH FROM Maillist into @emailAddress, @quoteref

In both FETCH lines.

Took me a while but I figured it out.

Thanks..!
0
 
LVL 8

Expert Comment

by:Bob Hoffman
ID: 33532928
great!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
This shares a stored procedure to retrieve permissions for a given user on the current database or across all databases on a server.
Integration Management Part 2
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question