SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 dynamic recipients in DB mail
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

s_anr
Yak Posting Veteran

81 Posts

Posted - 09/25/2010 :  16:50:39  Show Profile  Reply with Quote
Table importedrecords

Name Item email
Joe 03 joe@company.com
peter 05 peter@company.com
Adam 07 adam@cpmpany.com


EXEC msdb.dbo.sp_send_dbmail 
@profile_name = 'Administrator', 
@recipients = 'joe@company.com; peter@company.com; adam@company.com', 
@query = 'SELECT Name, Item FROM db.dbo.importedrecords' , 
@subject = 'Imported Records Count', 
@attach_query_result_as_file = 1 ;


I need to hard code the recepients. How can i do a dynamic search of email address?

So if there were only two names (joe / peter), email will just go to just two of them and lets say if a new record is added (John), now the emil should go to four recipients.

Can anyone help me make it dynamic? Thank you!!

visakh16
Very Important crosS Applying yaK Herder

India
47173 Posts

Posted - 09/26/2010 :  02:15:25  Show Profile  Reply with Quote
you can just use like:-

DECLARE @EmailList varchar(3000)
SELECT @EmailList = COALESCE(@EmailList,'') + email + ';'
FROM importedrecords

EXEC msdb.dbo.sp_send_dbmail 
@profile_name = 'Administrator', 
@recipients = @EmailList, 
@query = 'SELECT Name, Item FROM db.dbo.importedrecords' , 
@subject = 'Imported Records Count', 
@attach_query_result_as_file = 1 ;


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/


Edited by - visakh16 on 09/26/2010 02:15:52
Go to Top of Page

s_anr
Yak Posting Veteran

81 Posts

Posted - 09/26/2010 :  03:52:11  Show Profile  Reply with Quote
hi Visakh,

I get the following error :

Msg 22050, Level 16, State 1, Line 0
Error formatting query, probably invalid parameters
Msg 14661, Level 16, State 1, Procedure sp_send_dbmail, Line 478
Query execution failed: ?Msg 208, Level 16, State 1, Server MYSERVER, Line 1
Invalid object name 'importedrecords'


Note : select * from importedrecords is a valid query with 'email' as a valid field and it does give me the results when run independently.

Edited by - s_anr on 09/26/2010 03:54:33
Go to Top of Page

s_anr
Yak Posting Veteran

81 Posts

Posted - 09/28/2010 :  02:54:41  Show Profile  Reply with Quote
I'd really appreciate f someone can help. Its probably a syntax error which is causing the issue.
Go to Top of Page

s_anr
Yak Posting Veteran

81 Posts

Posted - 09/29/2010 :  23:17:41  Show Profile  Reply with Quote
Any suggestions please?
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
47173 Posts

Posted - 09/30/2010 :  11:53:12  Show Profile  Reply with Quote
quote:
Originally posted by s_anr

hi Visakh,

I get the following error :

Msg 22050, Level 16, State 1, Line 0
Error formatting query, probably invalid parameters
Msg 14661, Level 16, State 1, Procedure sp_send_dbmail, Line 478
Query execution failed: ?Msg 208, Level 16, State 1, Server MYSERVER, Line 1
Invalid object name 'importedrecords'


Note : select * from importedrecords is a valid query with 'email' as a valid field and it does give me the results when run independently.


are you running it in same db? else you need to add db.dbo.importedrecords

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000