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;
相关推荐
麦聪聊数据26 分钟前
利用实时数据管道与 SQL2API 重构企业自动化审计架构
数据库·sql·低代码
麦聪聊数据27 分钟前
重构开放生态:利用 QuickAPI 跨越遗留系统与敏捷交付的工程实践
数据库·sql·低代码·restful
百结2145 小时前
Mysql数据库操作
数据库·mysql·oracle
keep one's resolveY5 小时前
时区问题解决
数据库
Leinwin6 小时前
OpenClaw 多 Agent 协作框架的并发限制与企业化规避方案痛点直击
java·运维·数据库
qq_417695056 小时前
机器学习与人工智能
jvm·数据库·python
漫随流水6 小时前
旅游推荐系统(view.py)
前端·数据库·python·旅游
ego.iblacat6 小时前
MySQL 服务基础
数据库·mysql
Maverick067 小时前
Oracle Redo 日志操作手册
数据库·oracle
攒了一袋星辰8 小时前
高并发强一致性顺序号生成系统 -- SequenceGenerator
java·数据库·mysql