期初余额与发生额循环相加的游标
我的数据库中有两张表,如图:如今,我想把第二张表的期初余额作为第二张的期初余额
最后生成如图:
为此我写了一段游标代码:
declare @kmdm1 varchar(11),@qcye numeric(18,2)
declare yecur cursor for
select 科目代码,期初余额 from ceshiqcye
open yecur
fetch next from yecur into @kmdm1,@qcye
print '月份'+' '+'科目代码'+' '+'发生额'+' '+'余额'
while @@fetch_status=0
begin
print ' '+@kmdm1+' '+cast(@qcye as varchar(10))
declare @yf varchar(2),@kmdm2 varchar(11),@fse numeric(18,2),@id int,@ye numeric(18,2)
declare fscur cursor for
select 月份,科目编码,发生额,行数 from ceshifse
open fscur
fetch next from fscur into @yf,@kmdm2,@fse,@id
while @@FETCH_STATUS=0
begin
print @yf+@kmdm2+@fse+@ye
if
@id=1 set @ye=@qcye+@fse
else
set @ye=@ye+@fse
end
fetch next from fscur into @yf,@kmdm2,@fse,@id
end
close fscur
deallocate fscur
fetch next from yecur into @kmdm1,@qcye
end
close yecur
deallocate yecur
在SQL Server2008中运行,出现如下提示:
消息 156,级别 15,状态 1,第 29 行
关键字 'close' 附近有语法错误。
请各位大侠不吝赐教!!