Solved

sp_send_dbmail with query specific to each recipient

Posted on 2010-08-26
6
349 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 8

Accepted Solution

by:
Bob Hoffman earned 500 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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.

737 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