1. 程式人生 > >SQL SERVER中SQL中游標巢狀迴圈的示例

SQL SERVER中SQL中游標巢狀迴圈的示例

insert into #temp
select 1,2 
insert into #temp
select 3,4 
select * from #temp
declare @i int,
   @j int
declare cur1 cursor for
select a from #temp
open cur1
fetch cur1 into @i
while @@fetch_status =0
begin
select 'cur1:',@i
declare cur2 cursor for
   select b from #temp
open cur2
fetch cur2 into @j
while @@fetch_Status =0
begin
   select 'cur2:',@j
   fetch cur2 into @j
end
close cur2
deallocate cur2
fetch cur1 into @i
end
close cur1
drop table #temp
deallocate cur1