Solved

sp_send_dbmail with query specific to each recipient

Posted on 2010-08-26
6
339 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
Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

 
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

708 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now