SQL中游标嵌套循环的示例

SQL中经常要用到循环,下面就将为您介绍SQL中游标嵌套循环的示例,供您参考,希望对您学习SQL的循环有所帮助。

create table #temp(a int,b int)

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
 

 

【编辑推荐】

sql循环插入的语句写法

SQL循环语句的妙用

SQL中CONVERT函数详解

SQL中DateDiff函数的使用

删除SQL约束的方法

文章来源网络,作者:运维,如若转载,请注明出处:https://shuyeidc.com/wp/239274.html<

(0)
运维的头像运维
上一篇2025-04-23 08:14
下一篇 2025-04-23 08:15

相关推荐

发表回复

您的邮箱地址不会被公开。必填项已用 * 标注