I have a report that i run every month, below is the sql script for it. My problem currently is i have to run the sql script in text format and then save the result set into a tab delimited file and then save to excel. Right now on the excel it get everything even the rows it ran. I wanted to get everything in one result set. Please help. Thanks.
declare @xmid varchar(16)
declare @locationdba varchar(100)
declare @month varchar(2)
declare @year varchar(4)
set @month = '05'
set @year = '2011'
select distinct xmid,LOCATIONDBA into #tmpMIDS
from tblmerchants a
join tblmerchantapplications b on a.merchantid = b.merchantid
where locationdba like 'farmers furniture%'
SELECT 'TOTAL FOR ALL STORES'
SELECT 'INTERCHANGE : ALL STORES'
select ledgercode,substring(cast(ledgercode as varchar),5,3) FeeClass, mrev.description, sum(cnt) cnt, sum(volume) volume, MIC.RATE,MIC.PERITEM,
--case when substring(cast(ledgercode as varchar),4,1) = 1 then 0.000950 else 0.000925 end Assessments,
TotalDisc = sum(volume)* MIC.RATE --+ sum(volume) * case when substring(cast(ledgercode as varchar),4,1) = 1 then 0.000950 else 0.000925 end,
,totalPerItem = sum(cnt) * MIC.PERITEM ,
--TotalAmt = round(sum(TotalAmount),2,1)
TotalAmt = sum(TotalAmount)
from monthendrevshare mrev
join dbo.MonthendInterchange mIC on mrev.ledgercode = (900000000 + (mic.CardType * 100000) + (ICCode * 100) + 1 ) and enddate is null
where ledgercode > 900000 and xmid in (select xmid from #tmpMIDS )
and entrymonth = @month and entryyear = @year AND ROOTPORTFOLIOACCOUNTNUMBER = '1'
group by ledgercode,mrev.description, MIC.RATE,MIC.PERITEM
having sum(volume) > 0
order by volume desc
SELECT 'CREDIT INTERCHANGE : ALL STORES'
select ledgercode, substring(cast(ledgercode as varchar),5,3) FeeClass, mrev.description, sum(cnt) cnt, sum(volume) volume, MIC.RATE,MIC.PERITEM,
--case when substring(cast(ledgercode as varchar),4,1) = 1 then 0.000950 else 0.000925 end Assessments,
TotalDisc = sum(volume)* MIC.RATE --+ sum(volume) * case when substring(cast(ledgercode as varchar),4,1) = 1 then 0.000950 else 0.000925 end,
,totalPerItem = sum(cnt) * MIC.PERITEM,
--TotalAmt = round(sum(TotalAmount),2,1)
TotalAmt = sum(TotalAmount)
from monthendrevshare mrev
join dbo.MonthendInterchange mIC on mrev.ledgercode = (900000000 + (mic.CardType * 100000) + (ICCode * 100) + 1 ) and enddate is null
where ledgercode > 900000 and xmid in ( select xmid from #tmpMIDS )
and entrymonth = @month and entryyear = @year AND ROOTPORTFOLIOACCOUNTNUMBER = '1'
group by ledgercode,mrev.description, MIC.RATE,MIC.PERITEM
having sum(volume) <= 0
order by volume
SELECT 'AUTHORIZATIONS : ALL STORES'
select ledgercode, mrev.description, sum(cnt) cnt, AVG(PERITEM) PERITEM, SUM(TOTALAMOUNT) TOTAL
from monthendrevshare mrev
where ledgergroup = 11
and ledgercode not in (30,102)
and xmid in (select xmid from #tmpMIDS)
and entrymonth = @month and entryyear = @year AND ROOTPORTFOLIOACCOUNTNUMBER = '1'
group by ledgercode,mrev.description
having SUM(TOTALAMOUNT) > 0
order by TOTAL desc
SELECT 'FEES : ALL STORES'
select ledgercode, mrev.description, sum(volume) Volume, sum(cnt) Count, AVG(RATE) RATE, AVG(PERITEM) PERITEM, SUM(TOTALAMOUNT) TOTAL
from monthendrevshare mrev
where (ledgergroup = 12 or ledgercode in (104,105,30,102,500,501,502,503,92,94) )
and ledgercode not in (33,34,46,79,47,133,48,123)
and xmid in (select xmid from #tmpMIDS)
and entrymonth = @month and entryyear = @year AND ROOTPORTFOLIOACCOUNTNUMBER = '1'
group by ledgercode,mrev.description
having SUM(TOTALAMOUNT) > 0
order by TOTAL desc
DECLARE Merchant_Cursor CURSOR FOR
select xmid,locationdba
from #tmpMIDS
OPEN Merchant_Cursor
FETCH NEXT FROM Merchant_Cursor into @XMID, @locationdba
WHILE @@FETCH_STATUS = 0
BEGIN
SELECT 'TOTAL FOR STORE : ' + @XMID +' : ' + @locationdba
SELECT 'INTERCHANGE FOR STORE : ' + @XMID +' : ' + @locationdba
select ledgercode,substring(cast(ledgercode as varchar),5,3) FeeClass, mrev.description, sum(cnt) cnt, sum(volume) volume, MIC.RATE,MIC.PERITEM,
--case when substring(cast(ledgercode as varchar),4,1) = 1 then 0.000950 else 0.000925 end Assessments,
TotalDisc = sum(volume)* MIC.RATE --+ sum(volume) * case when substring(cast(ledgercode as varchar),4,1) = 1 then 0.000950 else 0.000925 end,
,totalPerItem = sum(cnt) * MIC.PERITEM ,
--TotalAmt = round(sum(TotalAmount),2,1)
TotalAmt = sum(TotalAmount)
from monthendrevshare mrev
join dbo.MonthendInterchange mIC on mrev.ledgercode = (900000000 + (mic.CardType * 100000) + (ICCode * 100) + 1 ) and enddate is null
where ledgercode > 900000 and xmid = @xmid
and entrymonth = @month and entryyear = @year AND ROOTPORTFOLIOACCOUNTNUMBER = '1'
group by ledgercode,mrev.description, MIC.RATE,MIC.PERITEM
having sum(volume) > 0
order by volume desc
SELECT 'CREDIT INTERCHANGE FOR STORE : ' + @XMID +' : ' + @locationdba
select ledgercode, substring(cast(ledgercode as varchar),5,3) FeeClass, mrev.description, sum(cnt) cnt, sum(volume) volume, MIC.RATE,MIC.PERITEM,
--case when substring(cast(ledgercode as varchar),4,1) = 1 then 0.000950 else 0.000925 end Assessments,
TotalDisc = sum(volume)* MIC.RATE, --+ sum(volume) * case when substring(cast(ledgercode as varchar),4,1) = 1 then 0.000950 else 0.000925 end,
totalPerItem = sum(cnt) * MIC.PERITEM,
--TotalAmt = round(sum(TotalAmount),2,1)
TotalAmt = sum(TotalAmount)
from monthendrevshare mrev
join dbo.MonthendInterchange mIC on mrev.ledgercode = (900000000 + (mic.CardType * 100000) + (ICCode * 100) + 1 ) and enddate is null
where ledgercode > 900000 and xmid = @xmid
and entrymonth = @month and entryyear = @year AND ROOTPORTFOLIOACCOUNTNUMBER = '1'
group by ledgercode,mrev.description, MIC.RATE,MIC.PERITEM
having sum(volume) <= 0
order by volume
SELECT 'AUTHORIZATIONS FOR STORE : ' + @XMID +' : ' + @loc开发者_StackOverflow社区ationdba
select ledgercode, mrev.description, sum(cnt) cnt, AVG(PERITEM) PERITEM, SUM(TOTALAMOUNT) TOTAL
from monthendrevshare mrev
where ledgergroup = 11
and ledgercode not in (30,102)
and xmid = @xmid
and entrymonth = @month and entryyear = @year AND ROOTPORTFOLIOACCOUNTNUMBER = '1'
group by ledgercode,mrev.description
having SUM(TOTALAMOUNT) > 0
order by TOTAL desc
SELECT 'FEES FOR STORE : ' + @XMID +' : ' + @locationdba
select ledgercode, mrev.description, sum(volume) Volume, sum(cnt) Count, AVG(RATE) RATE, AVG(PERITEM) PERITEM, SUM(TOTALAMOUNT) TOTAL
from monthendrevshare mrev
where (ledgergroup = 12 or ledgercode in (104,105,30,102,500,501,502,503,92,94) )
and ledgercode not in (33,34,46,79,47,133,48,123)
and xmid = @xmid
and entrymonth = @month and entryyear = @year AND ROOTPORTFOLIOACCOUNTNUMBER = '1'
group by ledgercode,mrev.description
having SUM(TOTALAMOUNT) > 0
order by TOTAL desc
FETCH NEXT FROM Merchant_Cursor into @XMID, @locationdba
END
CLOSE Merchant_Cursor
DEALLOCATE Merchant_Cursor
drop table #tmpMIDS
I'm not going to rewrite that entire proc, but here's a method you can employ:
1 - use SET NOCOUNT ON
to disable the "xxx Rows Affected" messages.
2 - Keep your logic as is, but select into a "Results" table and at the end of the proc select the contents of "Results" as the only output. You can't really use a UNION
as it is now since you have different field names. If you NEED differing field names between result sets, then you won't ever be able to merge them into one final result in a functional way.
精彩评论