SQL server 2008 用游标查询两表连接语句,烦请各位帮忙!
1个回答
2012-06-15
展开全部
select a.sno,a.cno, sum(a.money)as money,count(a.cno) as shu into #table2
from table2 a,table2 b
where a.sno=b.sno and a.cno=b.cno and a.money=b.money
group by a.sno,a.cno
declare cur cursor
for
select a.sno, a.cno ,stasu ,money ,shu
from table1 a left join #table2 b
on a.sno=b.sno and a.cno=b.cno
open cur
go
declare @sno char(10),@cno varchar(10), @stasu varchar(10),@money varchar(10),@shu varchar(10)
fetch next from cur into @sno ,@cno, @stasu,@money,@shu
while(@@fetch_status=0)
begin
print +@sno +@cno+ @stasu+@money+'数量'+@shu
fetch next from cur into @sno ,@cno, @stasu,@money,@shu
end
close cur
deallocate cur
from table2 a,table2 b
where a.sno=b.sno and a.cno=b.cno and a.money=b.money
group by a.sno,a.cno
declare cur cursor
for
select a.sno, a.cno ,stasu ,money ,shu
from table1 a left join #table2 b
on a.sno=b.sno and a.cno=b.cno
open cur
go
declare @sno char(10),@cno varchar(10), @stasu varchar(10),@money varchar(10),@shu varchar(10)
fetch next from cur into @sno ,@cno, @stasu,@money,@shu
while(@@fetch_status=0)
begin
print +@sno +@cno+ @stasu+@money+'数量'+@shu
fetch next from cur into @sno ,@cno, @stasu,@money,@shu
end
close cur
deallocate cur
追问
执行后显示cur游标不存在
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询