Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
skaswani
Starting Member
24 Posts |
Posted - 2008-12-07 : 19:55:34
|
| Dear Friends, how are u alli am using SQL SERVER 2005 + Database Mail option====================================================i have this codeEXEC msdb.dbo.sp_send_dbmail @profile_name = 'scn', @recipients = 'scn@merakdemo.com', @query = 'SELECT refno,name,amount FROM table1 where status = "Pending"' , @subject = 'Pending Status', @attach_query_result_as_file = 1 ;what i want to do now,i want to achieve 2 goals,1st, i have a table table1 which has a field call status and what i want to do is check all pending records and send email to supervisorsupervisor email is in user_info table (this table will have user_id field)2nd, after 1/2 hour (which i'll manage by SQL agent) i want to mail branch manager and IT manager (which is in Branch_info table and it has a BR_CODE field and table Table1 also have the same field)please help me in this mattersample table structure is like this :-1) Table1 refno, name, amount, user_id, br_code2) user_info user_id, user_name3) branch_infoBr_code, ops_manager_email, it_manager_emailregards |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-12-07 : 20:40:40
|
| you need to identify and put the required email addresses onto a temporary table. then loop through table to grab each address and then use sp_send_dbmail to sent mail to each. You might need to use dynamic sql for dynamically determining email addresses from the temporary table. |
 |
|
|
skaswani
Starting Member
24 Posts |
Posted - 2008-12-08 : 07:07:20
|
| hi visakh16 and alli have applied this and its working fine without any error, but the email i record is emptyplease guide me thank----------------drop table #mail_mergegocreate table #mail_merge(customer_name varchar(20),address varchar(20),city varchar(20),email_addy varchar(30),email_body varchar(max))goinsert #mail_merge(customer_name ,address,city ,email_addy)select cust_name,cust_add,city,email from customergoupdate #mail_mergeset email_body= (select * from OpenRowset(Bulk 'F:\Test_Letter.htm', Single_Clob) as letter_template)goupdate #mail_mergeset email_body = replace(email_body,'Customer_Name',customer_name )goupdate #mail_mergeset email_body = replace(email_body,'Customer_Address',address )goupdate #mail_mergeset email_body = replace(email_body,'Customer_City',city )declare @email_addy varchar(225)declare @email_body nvarchar(max)declare mail_merge cursor for select email_addy,email_body from #mail_mergeopen mail_mergefetch next from mail_merge into @email_addy, @email_bodywhile @@fetch_status = 0beginexecute msdb.dbo.sp_send_dbmail@profile_name = 'Scn',@recipients = @email_addy,@subject = 'customer info',@body_format = 'HTML'fetch next from mail_merge into @email_addy, @email_bodyendclose mail_mergedeallocate mail_mergedrop table #mail_mergego |
 |
|
|
skaswani
Starting Member
24 Posts |
Posted - 2008-12-08 : 08:06:32
|
| i dont know i am unable to delete my last reply,i think i need to recheck and ree-write my codethen get back to u |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-12-08 : 08:34:20
|
quote: Originally posted by skaswani i dont know i am unable to delete my last reply,i think i need to recheck and ree-write my codethen get back to u
yup...try rewriting it and let us know if you face any issues |
 |
|
|
skaswani
Starting Member
24 Posts |
Posted - 2008-12-08 : 10:31:07
|
| hii am back:)i have modified my code as per my needbut yet i dont see any record in emailplease help medrop table branch_infogocreate table branch_info(br_code int,ops_manager_email varchar(40),it_manager_email varchar(40))drop table user_infogocreate table user_info(user_code int,supervisor_email varchar(40),br_code int)drop table scn_detailsgocreate table scn_details(refno int,ben_name varchar(30),amount numeric(8,0),status varchar(10),br_code int, user_code int)create table #mail_merge(refno int,ben_name varchar(40),amount numeric(8,0),br_code int, user_code int,supervisor_email varchar(40),email_body varchar(max))goinsert #mail_merge(refno ,ben_name,amount ,br_code,user_code,supervisor_email)select s.refno,s.ben_name,s.amount ,s.br_code, s.user_code, u.supervisor_email from scn_details s, user_info uwhere s.status = 'Pending' and s.user_code=u.user_codegodeclare @supervisor_email varchar(225)declare @email_body nvarchar(max)declare mail_merge cursor for select supervisor_email,email_body from #mail_mergeopen mail_mergefetch next from mail_merge into @supervisor_email, @email_bodywhile @@fetch_status = 0beginexecute msdb.dbo.sp_send_dbmail@profile_name = 'Scn',@recipients = @supervisor_email,@subject = 'SCN - pending Records',@body_format = 'HTML'fetch next from mail_merge into @supervisor_email, @email_bodyendclose mail_mergedeallocate mail_mergedrop table #mail_mergego(1 row(s) affected)output===========select * from #mail_merge150 Sajjad 15000 10 100 scn@merakdemo.com NULLregards |
 |
|
|
skaswani
Starting Member
24 Posts |
Posted - 2008-12-08 : 12:24:19
|
| dear visakh16i think i am near to what i want to achieve, please last thingcreate table mail_merge(refno int,ben_name varchar(40),amount numeric(8,0),br_code int, user_code int,supervisor_email varchar(40),email_body varchar(max))goinsert mail_merge(refno ,ben_name,amount ,br_code,user_code,supervisor_email)select s.refno,s.ben_name,s.amount ,s.br_code, s.user_code, u.supervisor_email from scn_details s, user_info uwhere s.status = 'Pending' and s.user_code=u.user_codegodeclare @supervisor_email varchar(225)declare @email_body nvarchar(max)declare mail_merge cursor for select supervisor_email,email_body from mail_mergeopen mail_mergefetch next from mail_merge into @supervisor_email, @email_bodywhile @@fetch_status = 0beginexecute msdb.dbo.sp_send_dbmail@profile_name = 'Scn',@recipients = @supervisor_email,@subject = 'SCN - Pending Records',@body_format = 'HTML',@query = 'Select * from mail_merge',@attach_query_result_as_file = 1 fetch next from mail_merge into @supervisor_email, @email_bodyendclose mail_mergedeallocate mail_mergedrop table mail_mergego===============================when this code execute it send all the supervisors all the pending records not the records which are related to them,like a user have one supervisor so his entry should be shown to his supervious only to avoid the messi'll eally appriciate if u help me in this regardthanks |
 |
|
|
skaswani
Starting Member
24 Posts |
Posted - 2008-12-09 : 06:23:15
|
| anyone? help me pls |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-12-09 : 10:38:09
|
You're not checking for any condition in query. i think it should beSelect * from mail_merge where {some condition here to filter on supervisor} |
 |
|
|
|
|
|
|
|