sql游标
请各位大虾帮忙解决我想将凭证表与余额表连接查询,并输出如下结果:但我在SQL Server2008中写如下游标代码:
declare @n int,@nd char(12),@kjqj char(2),@kmbm char(7),@kmmc char(14),@qcye numeric(8,2),@jffs numeric(8,2),@dffs numeric(8,2),@qmye numeric(8,2)
set @n=1 '用以储存行数
declare xj cursor for
Select a.年度,会计月份,a.科目编码,a.科目名称
,b.期初余额
,cast(sum(借方金额) as numeric(8,2)),cast(sum(贷方金额) as numeric(8,2))
,b.期初余额+cast(sum(借方金额) as numeric(8,2))-cast(sum(贷方金额) as numeric(8,2))
From [凭证库] a
join dbo.余额表 b on a.年度=b.年度 and a.科目编码=b.科目编码
WHERE a.科目编码 LIKE'101%' AND (a.年度=2011 or a.年度=2012) group by a.年度,会计月份,a.科目编码,a.科目名称,期初余额
order by a.年度,会计月份
open xj
print '年度 会计月份 科目编码 科目名称 期初余额 借方发生额 贷方发生额 期末余额'
fetch next from xj into @nd,@kjqj,@kmbm,@kmmc,@qcye,@jffs,@dffs,@qmye
while @@fetch_status=0
begin
if @kjqj=1
print @nd+@kjqj+' '+@kmbm+' '+@kmmc+' '+cast(@qcye as char(10))+' '+cast(@jffs as char(10))+' '+cast(@dffs as char(10))+' '+cast(@qmye as char(10))
else
begin
print @nd+@kjqj+' '+@kmbm+' '+@kmmc+' '+cast(@qmye as char(10))+' '+cast(@jffs as char(10))+' '+cast(@dffs as char(10))+' '+cast(@qmye as char(10))
end
fetch next from xj into @nd,@kjqj,@kmbm,@kmmc,@qcye,@jffs,@dffs,@qmye
end
close xj
deallocate xj
得到的结果却是如此:
大家看到后一定帮我修改,谢了!