?
Solved

SQLMail - Inserting a variable into @Recipients

Posted on 2003-03-13
3
Medium Priority
?
583 Views
Last Modified: 2008-02-01
I'd like to do something like this:

Select @mailuser = EmailAddress from ContactTable where (criteria blah blah)
exec xp_sendmail @recipients = @mailuser, @subject = 'Hi'

I keep getting a "A recipient was specified ambiguously" error..

I also tried this below thinking that maybe my select statement was the problem, but I get the same error..

Set @mailuser = 'SpecificEmailAddress@SpecificCompany.com'
exec xp_sendmail @recipients = @mailuser, @subject = 'Hi'

Does anyone know how I can use a variable for a parameter in sendmail?

 
0
Comment
Question by:Jer995
[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 Comments
 
LVL 69

Accepted Solution

by:
Scott Pletcher earned 1000 total points
ID: 8129287
I think you will have to use dynamic execution:

DECLARE @sql VARCHAR(2000)
SET @sql = 'EXEC xp_sendmail @recipients=''' + @mailuser + ''', @subject=''Hi'''
EXEC (@sql)
0
 
LVL 7

Expert Comment

by:TroyK
ID: 8129446
Jer;

If you go with a dynamic SQL solution, be sure to stringently validate the source of your data, in this case, the ContactTable.

Consider the following script (I changed EXEC to SELECT so that you can see the resulting SQL statement):

-------- BEGIN SQL Script --------
CREATE TABLE ContactTable(
  ArbitraryID int NOT NULL PRIMARY KEY,
  EmailAddress varchar(255)
)
GO

INSERT INTO ContactTable
SELECT 1, 'test@abc.com' UNION ALL
/* Imagine if the SELECT @@Version statement below
   was something harmful, such as 'SHUTDOWN', or DROP TABLE Foo */
SELECT 2, 'i@hacked.you''; SELECT @@Version--'
GO

DECLARE @sql VARCHAR(2000)
DECLARE @mailuser varchar(255)

SELECT @mailuser = EmailAddress FROM ContactTable WHERE ArbitraryID = 1
SET @sql = 'EXEC xp_sendmail @recipients=''' + @mailuser + ''', @subject=''Hi'''
SELECT @sql

SELECT @mailuser = EmailAddress FROM ContactTable WHERE ArbitraryID = 2
SET @sql = 'EXEC xp_sendmail @recipients=''' + @mailuser + ''', @subject=''Hi'''
SELECT @sql
--------------- END SQL Script --------

HTH,
TroyK, MCSD
0
 

Author Comment

by:Jer995
ID: 8129908
This worked, Thanks for the help,
Jeremy

Declare @mailuser varchar(255)
Select @mailuser = EmailAddress FROM (table blah blah)where (criteria blah blah)
DECLARE @sql VARCHAR(2000)
SET @sql = 'EXEC xp_sendmail @recipients=''' + @mailuser + ''', @subject=''Hi'''
EXEC (@sql)
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
I have a large data set and a SSIS package. How can I load this file in multi threading?
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Suggested Courses

770 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