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;
相关推荐
岳麓丹枫00131 分钟前
PostgreSQL 中 pg_wal 目录里的 .ready .done .history 文件的生命周期
数据库·postgresql
陌上丨7 小时前
Redis的Key和Value的设计原则有哪些?
数据库·redis·缓存
AI_56787 小时前
AWS EC2新手入门:6步带你从零启动实例
大数据·数据库·人工智能·机器学习·aws
ccecw7 小时前
Mysql ONLY_FULL_GROUP_BY模式详解、group by非查询字段报错
数据库·mysql
JH30737 小时前
达梦数据库与MySQL的核心差异解析:从特性到实践
数据库·mysql
数据知道8 小时前
PostgreSQL 核心原理:如何利用多核 CPU 加速大数据量扫描(并行查询)
数据库·postgresql
麦聪聊数据9 小时前
Web 原生架构如何重塑企业级数据库协作流?
数据库·sql·低代码·架构
未来之窗软件服务9 小时前
数据库优化提速(四)新加坡房产系统开发数据库表结构—仙盟创梦IDE
数据库·数据库优化·计算机软考
Goat恶霸詹姆斯10 小时前
mysql常用语句
数据库·mysql·oracle
大模型玩家七七11 小时前
梯度累积真的省显存吗?它换走的是什么成本
java·javascript·数据库·人工智能·深度学习