Author |
Topic |
dtrivedi
Posting Yak Master
153 Posts |
Posted - 2010-07-20 : 14:03:24
|
This query run fine. it sends me the emails that i need.here is the query:use ANALYTICScreate table #ezpayloc(locations varchar(50), storeemail varchar(50))insert into #ezpayloc SELECT distinct [LOCATION], +('mac'+(location)+'@maclocal.com')FROM vw_PAST_DUE_LESSONS_EZPAY] order by locationselect * from #ezpaylocSELECT distinct [LOCATION], +('mac'+(location)+'@maclocal.com')FROM vw_PAST_DUE_LESSONS_EZPAY] order by locationGODECLARE @locs varchar(max)declare @email varchar(max)declare t_ezpay cursor for select storeemail from #ezpaylocdeclare t_ezpay cursor for select locations from #ezpaylocopen t_ezpayfetch next from t_ezpay into @emailfetch next from t_ezpay into @locswhile @@FETCH_STATUS = 0BEGINPrint 'Processing: ' + @emailEXEC msdb.dbo.sp_send_dbmail@recipients = 'dtrivedi@musicarts.com',@body= 'All attempts to charge the credit card on file for the following EZ Pay Customers has failed. Please contact the customer, update the card on file and charge the balance to the new card.',@query='SELECT [LOCATION],[CUSTOMER],[ACCOUNT],[CUSTOMER_NAME],[PHONE] FROM vw_PAST_DUE_LESSONS_EZPAY]' ,@subject = 'EZ Pay' fetch next from t_ezpayinto @emailENDclose t_ezpaydeallocate t_ezpaydrop table #ezpaylocnow what i need it to do is with each time the query fetches i need it to take the results and email it to the location specific to itfor ex if it runs location cutomer 0011 12340011 12510011 42500012 15060012 1508so when it send an email to location 0011, location 0011 should only get their info as it the first 3 resultsdoes that make sense? |
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
dtrivedi
Posting Yak Master
153 Posts |
Posted - 2010-07-20 : 15:09:23
|
thanks alot! |
 |
|
dtrivedi
Posting Yak Master
153 Posts |
Posted - 2010-07-20 : 15:17:17
|
where does the set @query command go...im getting this errorMsg 22050, Level 16, State 1, Line 0Error formatting query, probably invalid parametersMsg 14661, Level 16, State 1, Procedure sp_send_dbmail, Line 504Query execution failed: Msg 137, Level 15, State 2, Server DWH01, Line 2Must declare the scalar variable "@locs". |
 |
|
dtrivedi
Posting Yak Master
153 Posts |
Posted - 2010-07-20 : 15:18:15
|
THIS IS WHAT IT LOOKS LIKE RIGHT NOW! use ANALYTICS create table #ezpayloc (locations varchar(50), storeemail varchar(50))insert into #ezpayloc SELECT distinct [LOCATION], +('mac'+(location)+'@maclocal.com') FROM [ANALYTICS].[dbo].[vw_PAST_DUE_LESSONS_EZPAY] order by location select * from #ezpayloc SELECT distinct [LOCATION], +('mac'+(location)+'@maclocal.com') FROM vw_PAST_DUE_LESSONS_EZPAY] order by locationGODECLARE @locs varchar(max)DECLARE @query varchar(1000)declare @email varchar(max)declare t_ezpay cursor for select storeemail from #ezpaylocdeclare t_ezpay cursor for select locations from #ezpaylocopen t_ezpayset @query='SELECT [LOCATION],[CUSTOMER],[ACCOUNT],[CUSTOMER_NAME],[PHONE] FROM vw_PAST_DUE_LESSONS_EZPAY] where location=@locs'fetch next from t_ezpay into @emailfetch next from t_ezpay into @locs while @@FETCH_STATUS = 0 BEGIN Print 'Processing: ' + @email EXEC msdb.dbo.sp_send_dbmail@recipients = 'dtrivedi@musicarts.com',@body= 'All attempts to charge the credit card on file for the following EZ Pay Customers has failed. Please contact the customer, update the card on file and charge the balance to the new card.',@query=@query,@subject = 'EZ Pay' fetch next from t_ezpay into @email ENDclose t_ezpaydeallocate t_ezpaydrop table #ezpayloc |
 |
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
dtrivedi
Posting Yak Master
153 Posts |
Posted - 2010-07-20 : 16:49:10
|
ok how's this use ANALYTICS create table #ezpayloc (locations varchar(50), storeemail varchar(50))insert into #ezpayloc SELECT distinct [LOCATION], +('mac'+(location)+'@maclocal.com') FROM [ANALYTICS].[dbo].[vw_PAST_DUE_LESSONS_EZPAY] order by location select * from #ezpayloc SELECT distinct [LOCATION], +('mac'+(location)+'@maclocal.com') FROM [ANALYTICS].[dbo].[vw_PAST_DUE_LESSONS_EZPAY] order by locationGODECLARE @locs nvarchar(max)DECLARE @query nvarchar(1000)declare @email nvarchar(max)declare @recepients nvarchar(1000)declare @body nvarchar(1000)declare @subject nvarchar(1000)declare t_ezpay cursor for select storeemail, locations from #ezpaylocopen t_ezpayset @query='SELECT [LOCATION],[CUSTOMER],[ACCOUNT],[CUSTOMER_NAME],[PHONE] FROM [ANALYTICS].[dbo].[vw_PAST_DUE_LESSONS_EZPAY] where location=@locs'fetch next from t_ezpay into @emailfetch next from t_ezpay into @locs while @@FETCH_STATUS = 0 BEGIN Print 'Processing: ' + @email EXEC msdb.dbo.sp_send_dbmail@recipients = 'dtrivedi@musicarts.com',@body= 'All attempts to charge the credit card on file for the following EZ Pay Customers has failed. Please contact the customer, update the card on file and charge the balance to the new card.',@query=@query,@subject = 'EZ Pay' fetch next from t_ezpay into @email ENDclose t_ezpaydeallocate t_ezpaydrop table #ezpayloc |
 |
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2010-07-20 : 16:53:21
|
Try like this:use ANALYTICScreate table #ezpayloc(locations varchar(50), storeemail varchar(50))insert into #ezpayloc SELECT distinct [LOCATION], +('mac'+(location)+'@maclocal.com')FROM [ANALYTICS].[dbo].[vw_PAST_DUE_LESSONS_EZPAY] order by locationDECLARE @locs nvarchar(max)DECLARE @query nvarchar(1000)declare @email nvarchar(max)declare @recepients nvarchar(1000)declare @body nvarchar(1000)declare @subject nvarchar(1000)declare t_ezpay cursor for select storeemail, locations from #ezpaylocopen t_ezpayfetch next from t_ezpay into @email, @locswhile @@FETCH_STATUS = 0BEGINPrint 'Processing: ' + @emailset @query='SELECT [LOCATION],[CUSTOMER],[ACCOUNT],[CUSTOMER_NAME],[PHONE] FROM [ANALYTICS].[dbo].[vw_PAST_DUE_LESSONS_EZPAY] where location= ' + @locsEXEC msdb.dbo.sp_send_dbmail@recipients = 'dtrivedi@musicarts.com',@body= 'All attempts to charge the credit card on file for the following EZ Pay Customers has failed. Please contact the customer, update the card on file and charge the balance to the new card.',@query=@query,@subject = 'EZ Pay' fetch next from t_ezpayinto @email, @locsENDclose t_ezpaydeallocate t_ezpaydrop table #ezpayloc Tara KizerMicrosoft MVP for Windows Server System - SQL Serverhttp://weblogs.sqlteam.com/tarad/Subscribe to my blog |
 |
|
dtrivedi
Posting Yak Master
153 Posts |
Posted - 2010-07-21 : 08:23:13
|
thank you so much that worked perfect. i actually almost had it :)now when its send the emails the email looks like this All attempts to charge the credit card on file for the following EZ Pay Customers has failed. Please contact the customer, update the card on file and charge the balance to the new card.LOCATION CUSTOMER ACCOUNT CUSTOMER_NAME PHONE ---------- --------------- --------------- ------------------------------ --------------------7340 7340A0003935 5AD07 DANA BAKER 6317154814 7340 734000A04385 5ER22 RAY MAY 6314511250 (2 rows affected)NOW IS THERE A WAY TO PUT MORE SPACE BETWEEN THE QUERY RESULT AND THE BODY OF THE EMAIL IT SELFALSO IS THERE A WAY TO ELIMINATE ON THE BOTTOM WHERE IT SAYS (2 ROWS AFFECTED)?? |
 |
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2010-07-21 : 13:38:09
|
If you want to format the email, then you are going to need to look into the HTML formatting for Database Mail. Check sp_send_dbmail in SQL Server Books Online for details. BOL even has an example. I prefer to use bcp.exe to create the file just how I want it, and then I use sp_send_dbmail to attach my file. I use this method when I care about the formatting such as csv data. I use your method when I don't care about the formatting.Tara KizerMicrosoft MVP for Windows Server System - SQL Serverhttp://weblogs.sqlteam.com/tarad/Subscribe to my blog |
 |
|
dtrivedi
Posting Yak Master
153 Posts |
Posted - 2010-07-21 : 13:44:52
|
thanks tara it seems as though my boss doesn't want to use html but wants to have it formatcan he have his cake and eat it too? |
 |
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
dtrivedi
Posting Yak Master
153 Posts |
Posted - 2010-07-21 : 14:18:42
|
hmph well thanks i will talk to him and see what he wants me to do this is a career change for me and my 1st week on the job i guess it only get harder n harderthanks for ur help |
 |
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|