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;
相关推荐
不总是5 小时前
[2026最新] Windows 免安装版 MySQL 8 详细安装配置教程(ZIP 压缩包版)
数据库·windows·mysql
tedcloud1236 小时前
DBX部署教程:打造支持AI SQL助手的数据库管理环境
数据库·人工智能·sql
野生技术架构师6 小时前
我有个大胆的想法,用 PostgreSQL 代替 Redis
数据库·redis·postgresql
cfm_29146 小时前
Redis ZSet 有序集合详解
数据库·redis·缓存
瀚高PG实验室6 小时前
V4.5.6.1授予普通用户监控类系统表及视图的查询权限
数据库·瀚高数据库
BullSmall6 小时前
模板库与抽取实例:企业数据同步最佳实践
数据库
云策数链6 小时前
用友U8数据库核心表结构与业务关联解析(附常用查询SQL)
数据库·sql·erp·用友·云策数链
徒手猫6 小时前
MySQL 窗口函数完全指南
数据库·mysql
betazhou7 小时前
电科金仓数据库V9 MySQL兼容版本搭建一主一从体验
数据库·mysql·oracle·主从·高可用·kingbase·v9 mysql兼容版本
python在学ing7 小时前
Django框架学习笔记:从零基础到项目实战
数据库·python·django·sqlite