Solved

sp_send_dbmail with query specific to each recipient

Posted on 2010-08-26
6
350 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
Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

 
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

Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

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…
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 video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

728 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