试算平衡表的存储过程修改版

CREATE PROCEDURE dbo.UF_Rpt_TryCalculateBalance(
@acct as nvarchar(10)=null
,@unit1 as nvarchar(30)=null
,@unit2 as nvarchar(30)=null
,@unit3 as nvarchar(30)=null
,@unit4 as nvarchar(30)=null
,@su1 as bit=0
,@su2 as bit=0
,@su3 as bit=0
,@su4 as bit=0
,@year as int=null
,@period as int=null
,@unposted as bit=0
,@LocalId as varchar(50)=null
)as
begin

Declare
@sql nvarchar(4000)
,@groupby nvarchar(1000)
,@table nvarchar(2000)
,@addcol nvarchar(200)

set @groupby='acct'
set @addcol=',null as acct_unit1,null as acct_unit2,null as acct_unit3,null as acct_unit4'

if @su1=1
begin
set @groupby=@groupby + ',acct_unit1'
set @addcol=replace(@addcol,',null as acct_unit1','')
end
if @su2=1
begin
set @groupby=@groupby + ',acct_unit2'
set @addcol=replace(@addcol,',null as acct_unit2','')
end
if @su3=1
begin
set @groupby=@groupby + ',acct_unit3'
set @addcol=replace(@addcol,',null as acct_unit3','')
end
if @su4=1
begin
set @groupby=@groupby + ',acct_unit4'
set @addcol=replace(@addcol,',null as acct_unit4','')
end

set @table='Ledger'
if @unposted=1
set @table='(select acct,acct_unit1,acct_unit2,acct_unit3,acct_unit4,curr_code,for_amount,dom_amount,control_year,control_period,from_id from ledger
union
select acct,acct_unit1,acct_unit2,acct_unit3,acct_unit4,curr_code,for_amount,dom_amount,control_year,control_period,id as from_id from journal) a'

set @sql=
'select ' + @groupby + @addcol + ',curr_code
,sum(case when (for_amount>=0 and control_year=' + str(@year) + ' and control_period= ' + str(@period) + ') then isnull(for_amount,0) else 0 end) as foramt_dr
,sum(case when (for_amount<0 and control_year=' + str(@year) + ' and control_period= ' + str(@period) + ') then isnull(for_amount,0) else 0 end) as foramt_cr
,sum(case when (dom_amount>=0 and control_year=' + str(@year) + ' and control_period= ' + str(@period) + ') then isnull(dom_amount,0) else 0 end) as domamt_dr
,sum(case when (dom_amount<0 and control_year=' + str(@year) + ' and control_period= ' + str(@period) + ') then isnull(dom_amount,0) else 0 end) as domamt_cr
,sum(case when (control_year<=' + str(@year) + ' and control_period<=' + str(@period) + ') then isnull(for_amount,0) else 0 end) as end_foramt
,sum(case when (control_year<=' + str(@year) + ' and control_period<=' + str(@period) + ') then isnull(dom_amount,0) else 0 end) as end_domamt
from ' + @table +'
where acct=' +case when @acct is null then 'acct' else ''''+@acct+'''' end +'
and from_id=' +case when @Localid is null then 'from_id' else ''''+@Localid+'''' end +'

and isnull(acct_unit1,1)=' +case when @unit1 is null then 'isnull(acct_unit1,1)' else ''''+@unit1+'''' end +'
and isnull(acct_unit2,1)=' +case when @unit2 is null then 'isnull(acct_unit2,1)' else ''''+@unit2+'''' end +'
and isnull(acct_unit3,1)=' +case when @unit3 is null then 'isnull(acct_unit3,1)' else ''''+@unit3+'''' end +'
and isnull(acct_unit4,1)=' +case when @unit4 is null then 'isnull(acct_unit4,1)' else ''''+@unit4+'''' end +'
group by ' + @groupby + ',curr_code'

--IF object_id('[dbo].uf_tmp') IS NOT NULL --判断临时表#tmp是否存在,存在则删除
-- drop table uf_tmp

EXEC (@sql)

end
GO

----------------------------------------------------------------------------------------------------------------

where acct=' +case when @acct is null then 'acct' else ''''+@acct+'''' end +'
and from_id=' +case when @Localid is null then 'from_id' else ''''+@Localid+'''' end +'

and isnull(acct_unit1,1)=' +case when @unit1 is null then 'isnull(acct_unit1,1)' else ''''+@unit1+'''' end +'
and isnull(acct_unit2,1)=' +case when @unit2 is null then 'isnull(acct_unit2,1)' else ''''+@unit2+'''' end +'
and isnull(acct_unit3,1)=' +case when @unit3 is null then 'isnull(acct_unit3,1)' else ''''+@unit3+'''' end +'
and isnull(acct_unit4,1)=' +case when @unit4 is null then 'isnull(acct_unit4,1)' else ''''+@unit4+'''' end +'

-------------------------------------------------------------------------------------------------------------------

对字符型必须

字段='条件' 在拼接时候 需要"" ""


你可能感兴趣的:(存储过程)