sql 双游标循环

需要有俩个参数来执行双循环,但是写好怎么调试都有些问题,只能循环一次,也查看了很多帖子,但是没有什么进展。突然想到尝试内循环中重复打开内游标,最后得到结论。

--注释部分是之前代码,只能内循环一次,即只执行外游标的第一个参数,后续不执行

sql 复制代码
declare @f1 int;
declare @f2 nchar(10);
declare cursor_name1 cursor for
select f2 from table2
declare cursor_name2 cursor for 
	select f1 from table1 
open cursor_name1
--open cursor_name2 --此处不可添加,不然只内循环一次,挪到内循环里执行
fetch next from cursor_name1 into @f2 
while @@FETCH_STATUS=0 
begin
print(@f2)
	open cursor_name2 
	fetch next from cursor_name2 into @f1 
	while @@FETCH_STATUS=0 
	begin 
		declare @sql varchar(100);
		select @sql='所需前缀' + @f1 +','+@f2 ;--拼接一个sql
		print @sql;
		--exec (@sql) ;
		FETCH NEXT FROM cursor_name2 INTO @f1;
	end	
	CLOSE cursor_name2;
	
FETCH next from cursor_name1 into @f2;
end 
--CLOSE cursor_name2;--此处不可添加,不然只内循环一次,挪到内循环里执行
CLOSE cursor_name1;
DEALLOCATE cursor_name1;
DEALLOCATE cursor_name2;
相关推荐
XDHCOM20 小时前
ORA-32484重复列名错误,ORACLE数据库CYCLE子句故障修复与远程处理方案
数据库·oracle
翻斗包菜20 小时前
PostgreSQL 日常维护完全指南:从基础操作到高级运维
运维·数据库·postgresql
呆瑜nuage20 小时前
MySQL表约束详解:8大核心约束实战指南
数据库·mysql
liliangcsdn21 小时前
Agent Memory智能体记忆系统的示例分析
数据库·人工智能·全文检索
那个失眠的夜21 小时前
Mybatis延迟加载策略
xml·java·数据库·maven·mybatis
Rick199321 小时前
SQL 执行流程
数据库·sql
M--Y21 小时前
Redis常用数据类型
数据结构·数据库·redis
猿小喵21 小时前
MySQL慢查询分析与处理-第二篇
数据库·mysql·性能优化
Y001112361 天前
MySQL-进阶
开发语言·数据库·sql·mysql
徒 花1 天前
数据库知识复习01
数据库