這是一個存儲過程(至少我必須這樣假設),它構造一個Sql查詢爲一個String,然後用exec調用它。出於某種原因,自稱爲程序員的夥計,使用「〜」字符連接了一些值,並將其分割回應用程序層。這個SP打破時,一些複選框是「選擇」。問題是我怎樣才能調試這個噩夢。我發現當選中CheckBox時,它會在一些TypeCode的開頭添加一個不必要的「〜1」,並且它會中斷。如果沒有選擇,它按預期工作。 (順便說一句,我真的很想知道這種東西叫什麼樣的編碼實踐)。什麼是StoredProcedure
ALTER PROC [dbo].[BUTCEREP] @SirketKod varchar(10), @IlkTarih varchar(10), @SonTarih varchar(10), @Tip int, @Tur int, @MasrafMrk varchar(100), @DovizKod varchar(5), @MaliTKod varchar(10)
-- Tip 0-TarihAraligi; 1-2AylikButce; 2-YTDButce; 3-YTDCompPrev
-- Tur 0-Muhasebe 1-Gider 2-Butce 3-ButceMuhasebe 4-ButceGider
AS
declare
@SqlText nvarchar(4000),
@SqlBud varchar(4000),
@SqlMuh varchar(4000),
@SqlGid varchar(4000),
@xIlkT datetime,
@xSonT datetime,
@ThisMonth varchar(10),
@NextMonth varchar(10),
@ThisYear varchar(10),
@PrevYear varchar(10)
if (not @IlkTarih is null) set @xIlkT = CONVERT(datetime,@IlkTarih,103)
if (not @SonTarih is null) set @xSonT = CONVERT(datetime,@SonTarih,103)
set @DovizKod=IsNull(@DovizKod, '')
if @Tip=1 and MONTH(@xIlkT)=12 set @xIlkT = dateadd(month,-1,@xIlkT)
set @ThisMonth = CONVERT(varchar,MONTH(@xIlkT))
set @NextMonth = CONVERT(varchar,MONTH(@xIlkT)+1)
set @ThisYear = CONVERT(varchar,YEAR(@xIlkT))
set @PrevYear = CONVERT(varchar,YEAR(@xIlkT)-1)
set @SqlMuh='select X.MuhHesapKod,X.Tarih,SUM(CAST(X.Borc AS FLOAT)) Borc,'+
'SUM(CAST(X.Alacak AS FLOAT)) Alacak' +
' from (select CASE S.N WHEN 1 THEN LEFT(D.MuhHesapKod,3)' +
' WHEN 2 THEN LEFT(D.MuhHesapKod,7)' +
' ELSE D.MuhHesapKod END MuhHesapKod,' +
'D.Tarih,Borc,Alacak' +
' from '[email protected]+'MUH_D D inner join '[email protected]+'MUH_H H on (H.SirketKod=D.SirketKod) and (H.KontrolNo=D.KontrolNo)'+
' CROSS JOIN dbo.SeriTable(3) S' +
' where D.SirketKod=' + QuoteName(@SirketKod,'''') +
case when IsNull(@MasrafMrk,'')<>'' then ' and D.MasrafMrkKod in (select Kod from GetArray('+QuoteName(@MasrafMrk,'''')+'))' else '' end +
case @Tip
when 0 then ' and D.Tarih BETWEEN ' + IsNull(QuoteName(CONVERT(varchar,@xIlkT,103),''''),'D.Tarih') + ' and ' + IsNull(QuoteName(CONVERT(varchar,@xSonT,103),''''),'D.Tarih')
when 1 then ' and YEAR(D.Tarih)=' [email protected]+' and MONTH(D.Tarih) in ('[email protected]+','[email protected]+')'
when 2 then ' and YEAR(D.Tarih)=' [email protected]+' and MONTH(D.Tarih)<='[email protected]
when 3 then ' and (YEAR(D.Tarih)='[email protected]+' or (YEAR(D.Tarih)='[email protected]+' and MONTH(D.Tarih)<='[email protected]+'))'
end +
' and IsNull(H.FisTip,'''')<>''KAPANIS'''+
') X' +
' where X.MuhHesapKod in (select Kod from MALIT_M where SirketKod=' + QuoteName(@SirketKod,'''') +
' and MaliTKod=' + QuoteName(@MaliTKod,'''') + ')' +
' group by X.MuhHesapKod,X.Tarih'
set @SqlMuh = ' select 1 Tip,CAST(4 AS char(1))+''~0~''+M.MuhHesapKod Kod,' +
case @Tip
when 0 then 'SUM((M.Borc-M.Alacak)) TopBak'
when 1 then 'SUM(CASE WHEN MONTH(M.Tarih)=' [email protected]+' THEN 1 ELSE 0 END*(M.Borc-M.Alacak)) A1Bak,' +
'SUM(CASE WHEN MONTH(M.Tarih)=' [email protected]+' THEN 1 ELSE 0 END*(M.Borc-M.Alacak)) A2Bak'
when 2 then 'SUM(CASE WHEN MONTH(M.Tarih)=' [email protected]+' THEN 1 ELSE 0 END*(M.Borc-M.Alacak)) A1Bak,' +
'SUM(CASE WHEN MONTH(M.Tarih)<='[email protected]+' THEN 1 ELSE 0 END*(M.Borc-M.Alacak)) A2Bak'
when 3 then 'SUM(CASE WHEN YEAR(M.Tarih)=' [email protected]+' AND MONTH(M.Tarih)=' [email protected]+' THEN 1 ELSE 0 END*(M.Borc-M.Alacak)) A1Bak,' +
'SUM(CASE WHEN YEAR(M.Tarih)=' [email protected]+' AND MONTH(M.Tarih)<='[email protected]+' THEN 1 ELSE 0 END*(M.Borc-M.Alacak)) A2Bak,' +
'SUM(CASE WHEN YEAR(M.Tarih)=' [email protected]+' THEN 1 ELSE 0 END*(M.Borc-M.Alacak)) A3Bak'
end +
' from (' + @SqlMuh + ') M' +
' where M.MuhHesapKod in (select Kod from MALIT_M where SirketKod=' + QuoteName(@SirketKod,'''') +
' and MaliTKod=' + QuoteName(@MaliTKod,'''') + ')'+
' group by M.MuhHesapKod'
set @SqlGid = ' select 1 Tip,CAST(2 AS char(1))+''~0~''+C.GiderKod Kod,' +
case @Tip
when 0 then 'SUM((2*C.IslemTuru-1)*'+CASE WHEN @DovizKod='' THEN '(TutarTL+GVergiTutar)' ELSE 'GEURO' END+') TopBak'
when 1 then 'SUM(CASE WHEN MONTH(C.Tarih)='[email protected]+' THEN 1 ELSE 0 END*(2*C.IslemTuru-1)*'+CASE WHEN @DovizKod='' THEN '(TutarTL+GVergiTutar)' ELSE 'GEURO' END+') A1Bak,' +
'SUM(CASE WHEN MONTH(C.Tarih)='[email protected]+' THEN 1 ELSE 0 END*(2*C.IslemTuru-1)*'+CASE WHEN @DovizKod='' THEN '(TutarTL+GVergiTutar)' ELSE 'GEURO' END+') A2Bak'
when 2 then 'SUM(CAST(CASE WHEN MONTH(C.Tarih)=' [email protected]+ ' THEN 1 ELSE 0 END*(2*C.IslemTuru-1)*'+CASE WHEN @DovizKod='' THEN '(TutarTL+GVergiTutar)' ELSE 'GEURO' END+' AS FLOAT)) A1Bak,' +
'SUM(CAST(CASE WHEN MONTH(C.Tarih)<='[email protected]+ ' THEN 1 ELSE 0 END*(2*C.IslemTuru-1)*'+CASE WHEN @DovizKod='' THEN '(TutarTL+GVergiTutar)' ELSE 'GEURO' END+' AS FLOAT)) A2Bak'
when 3 then 'SUM(CAST(CASE WHEN YEAR(C.Tarih)=' [email protected]+' AND MONTH(C.Tarih)=' [email protected]+ ' THEN 1 ELSE 0 END*(2*C.IslemTuru-1)*'+CASE WHEN @DovizKod='' THEN '(TutarTL+GVergiTutar)' ELSE 'GEURO' END+' AS FLOAT)) A1Bak,' +
'SUM(CAST(CASE WHEN YEAR(C.Tarih)=' [email protected]+' AND MONTH(C.Tarih)<='[email protected]+ ' THEN 1 ELSE 0 END*(2*C.IslemTuru-1)*'+CASE WHEN @DovizKod='' THEN '(TutarTL+GVergiTutar)' ELSE 'GEURO' END+' AS FLOAT)) A2Bak,' +
'SUM(CAST(CASE WHEN YEAR(C.Tarih)=' [email protected]+' THEN 1 ELSE 0 END*(2*C.IslemTuru-1)*'+CASE WHEN @DovizKod='' THEN '(TutarTL+GVergiTutar)' ELSE 'GEURO' END+' AS FLOAT)) A3Bak'
end +
' from CHI_D C'+
' where C.GiderKod in (select Kod from MALIT_M where SirketKod=' + QuoteName(@SirketKod,'''') +
' and MaliTKod=' + QuoteName(@MaliTKod ,'''') + ')'+
case when IsNull(@MasrafMrk,'')<>'' then ' and C.MasrafMrkKod in (select Kod from GetArray('+QuoteName(@MasrafMrk,'''')+'))' else '' end +
case @Tip
when 0 then ' and C.Tarih BETWEEN ' + IsNull(QuoteName(CONVERT(varchar,@xIlkT,103),''''),'C.Tarih') + ' and ' + IsNull(QuoteName(CONVERT(varchar,@xSonT,103),''''),'C.Tarih') + ')'
when 1 then ' and YEAR(C.Tarih)='[email protected]+' and MONTH(C.Tarih) in (' [email protected]+','[email protected]+')'
when 2 then ' and YEAR(C.Tarih)='[email protected]+' and MONTH(C.Tarih)<='[email protected]
when 3 then ' and (YEAR(C.Tarih)='[email protected]+' or (YEAR(C.Tarih)='[email protected]+' and MONTH(C.Tarih)<='[email protected]+'))'
end +
' group by C.GiderKod'
set @SqlBud='select 0 Tip,CAST(D.IslemTipi AS char(1))+''~0~''+D.KalemKod Kod,' +
case @Tip
when 0 then 'sum((2*D.IslemTuru-1)*IsNull(D.Tutar,0)) TopBak'
when 1 then 'sum(CASE WHEN MONTH(D.BasTarih)='[email protected]+' THEN 1 ELSE 0 END*(2*D.IslemTuru-1)*IsNull(D.Tutar,0)) A1Bak,' +
'sum(CASE WHEN MONTH(D.BasTarih)='[email protected]+' THEN 1 ELSE 0 END*(2*D.IslemTuru-1)*IsNull(D.Tutar,0)) A2Bak'
when 2 then 'sum(CASE WHEN MONTH(D.BasTarih)='[email protected]+' THEN 1 ELSE 0 END*(2*D.IslemTuru-1)*IsNull(D.Tutar,0)) A1Bak,' +
'sum((2*D.IslemTuru-1)*IsNull(D.Tutar,0)) A2Bak'
when 3 then 'sum(CASE WHEN MONTH(D.BasTarih)=' [email protected]+' THEN 1 ELSE 0 END*(2*D.IslemTuru-1)*IsNull(D.Tutar,0)) A1Bak,' +
'sum(CASE WHEN MONTH(D.BasTarih)<='[email protected]+' THEN 1 ELSE 0 END*(2*D.IslemTuru-1)*IsNull(D.Tutar,0)) A2Bak,' +
'sum((2*D.IslemTuru-1)*IsNull(D.Tutar,0)) A3Bak'
end +
' from dbo.GetButceList('+ QuoteName(@SirketKod,'''') +','+ IsNull(QuoteName(@MasrafMrk,''''),'null')+','+QuoteName(@DovizKod,'''')+',0) D' +
' where D.KalemKod in (select Kod from MALIT_M where SirketKod=' + QuoteName(@SirketKod,'''') +
' and MaliTKod=' + QuoteName(@MaliTKod,'''') + ')'+
case @Tip
when 0 then ' and D.BasTarih>=' + IsNull(QuoteName(CONVERT(varchar,@xIlkT,103),''''),'D.BasTarih') + ' and D.BitTarih<=' + IsNull(QuoteName(CONVERT(varchar,@xSonT,103),''''),'D.BitTarih')
when 1 then ' and YEAR(D.BasTarih)='[email protected]+' and MONTH(D.BasTarih) in (' [email protected]+','[email protected]+')'
when 2 then ' and YEAR(D.BasTarih)='[email protected]+' and MONTH(D.BasTarih)<='[email protected]
when 3 then ' and YEAR(D.BasTarih)='[email protected]
end +
' group by D.KalemKod,D.IslemTipi'
-- Tur 0-Muhasebe 1-Gider 2-Butce 3-ButceMuhasebe 4-ButceGider
set @SqlText = case @Tur
when 0 then @SqlMuh
when 1 then @SqlGid
when 2 then @SqlBud
when 3 then @SqlBud + ' union all ' + @SqlMuh
when 4 then @SqlBud + ' union all ' + @SqlGid
end
if @Tip in (1,2)
set @SqlText='select Kod,' +
'SUM(CASE Tip WHEN 0 THEN IsNull(A1Bak,0) ELSE 0 END) A10Bak,' + --budget first
'SUM(CASE Tip WHEN 0 THEN IsNull(A2Bak,0) ELSE 0 END) A20Bak,' + --budget second
'SUM(CASE Tip WHEN 1 THEN IsNull(A1Bak,0) ELSE 0 END) A11Bak,' + --actual first
'SUM(CASE Tip WHEN 1 THEN IsNull(A2Bak,0) ELSE 0 END) A21Bak ' + --actual second
'from (' + @SqlText + ') X' +
' group by Kod'
else if @Tip = 3
set @SqlText='select Kod,' +
'SUM(CASE Tip WHEN 1 THEN IsNull(A3Bak,0) ELSE 0 END) A00Bak,' + --actual prev
'SUM(CASE Tip WHEN 0 THEN IsNull(A1Bak,0) ELSE 0 END) A10Bak,' + --budget first
'SUM(CASE Tip WHEN 0 THEN IsNull(A2Bak,0) ELSE 0 END) A20Bak,' + --budget second
'SUM(CASE Tip WHEN 1 THEN IsNull(A1Bak,0) ELSE 0 END) A11Bak,' + --actual first
'SUM(CASE Tip WHEN 1 THEN IsNull(A2Bak,0) ELSE 0 END) A21Bak,' + --actual second
'SUM(CASE Tip WHEN 0 THEN IsNull(A3Bak,0) ELSE 0 END) A01Bak ' + --budget yearly
'from (' + @SqlText + ') X' +
' group by Kod'
PRINT @SqlText
exec sp_executesql @SqlText
「問題是我該如何調試這個噩夢。」 - 像其他人一樣會這樣做。逐行瀏覽代碼,找出它的作用,用不同的參數對其進行測試,嘗試推斷業務規則並編寫錯誤修復程序或更換代碼(如果你敢的話)。 – Tomalak 2010-02-23 13:40:23
如果問題是如何調試這個噩夢,爲什麼標題是「什麼是StoredProcedure」? – Ricket 2010-02-23 13:58:22
只是意味着它很大... – AIC 2010-02-23 14:08:23