22,298
社区成员
发帖
与我相关
我的任务
分享CREATE PROCEDURE BMS_BUSINESS_UPDATEBYFORMULA
(
@COMPCODE NVARCHAR(5)
,@PERIOD NVARCHAR(4)
)
AS
BEGIN
SET NOCOUNT ON;
declare @code varchar(300),@sql varchar(8000),@fid varchar(300)
declare tc cursor for select fid from tb_formula
open tc
fetch next from tc into @fid
while @@fetch_status=0
begin
select @code=formula from tb_formula where fid=@fid
set @sql=''
select @sql=@sql+',['+itemid+']=max(case itemid when '''+itemid+''' then planamount end)' from tb_plan where compcode=@COMPCODE and period=@PERIOD
set @sql='update tb_plan set planamount=(select '+@code+' from (select '+stuff(@sql,1,1,'')+' from tb_plan where compcode='''+@COMPCODE+''' and period='''+@PERIOD+''' ) t)'
set @sql=@sql+'where compcode='''+@COMPCODE+''' and period='''+@PERIOD+''' and itemid='''+@fid+''''
exec(@sql)
fetch next from tc into @fid
end
close tc
deallocate tc
ENDdeclare @code varchar(30),@sql varchar(8000)
declare tc cursor for select formula from tb_formula
open tc
fetch next from tc into @code
while @@fetch_status=0
begin
set @sql=''
select @sql=@sql+',['+itemid+']=max(case itemid when '''+itemid+''' then planamount end)' from tb_plan where compcode='04770' and period='2008'
select @sql
set @sql='select '+@code+' from (select '+stuff(@sql,1,1,'')+' from tb_plan where compcode=''04770'' and period=''2008'' ) t'
select @sql
exec(@sql)
fetch next from tc into @code
end
close tc
deallocate tcselect @formula=formula from F where itemid=@itemid
select @formula=replace(@formula,‘Param1’,cast(t.Amount as varchar(10))
from F inner join t on F.Param1=t.itemid
where f.itemid=@itemid
select @formula=replace(@formula,‘Param2’,cast(t.Amount as varchar(10))
from F inner join t on F.Param2=t.itemid
where f.itemid=@itemid
select @sql='update t
set amount ='+@formula
+'where t.Itemid='''+@itemid+''''
exec @sql