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.

 All Forums
 General SQL Server Forums
 New to SQL Server Administration
 WHERE CLAUSE

Author  Topic 

dtrivedi
Posting Yak Master

153 Posts

Posted - 2010-07-20 : 11:53:56
This query run fine. it sends me the emails that i need.

here is the query:


use ANALYTICS

create 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 location

select * from #ezpayloc



SELECT distinct [LOCATION], +('mac'+(location)+'@maclocal.com')
FROM vw_PAST_DUE_LESSONS_EZPAY] order by location
GO

DECLARE @locs varchar(max)
declare @email varchar(max)

declare t_ezpay
cursor for select storeemail from #ezpayloc
declare t_ezpay
cursor for select locations from #ezpayloc
open t_ezpay

fetch next from t_ezpay into @email

fetch 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='SELECT [LOCATION],[CUSTOMER],[ACCOUNT],[CUSTOMER_NAME],[PHONE]
FROM vw_PAST_DUE_LESSONS_EZPAY]' ,
@subject = 'EZ Pay'


fetch next from t_ezpay
into @email

END

close t_ezpay
deallocate t_ezpay

drop table #ezpayloc

now 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 it

for ex if it runs
location cutomer
0011 1234
0011 1251
0011 4250
0012 1506
0012 1508

so when it send an email to location 0011, location 0011 should only get their info as it the first 3 results

does that make sense?
   

- Advertisement -