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;
相关推荐
v***5652 小时前
PostgreSQL 中进行数据导入和导出
大数据·数据库·postgresql
q***72564 小时前
Redis-配置文件
数据库·redis·oracle
不可描述的两脚兽4 小时前
Redis 快记
java·数据库·redis
h***34634 小时前
【MySQL】表的基本操作
数据库·mysql·oracle
SelectDB4 小时前
为什么实时更新场景下 Doris 查询性能是 ClickHouse 的 34 倍
数据库
n***63275 小时前
MySQL数据库的数据文件保存在哪?MySQL数据存在哪里
数据库·mysql
SelectDB5 小时前
从 Flink 到 Doris 的实时数据写入实践——基于 Flink CDC 构建更实时高效的数据集成链路
数据库
普通网友5 小时前
使用Flask快速搭建轻量级Web应用
jvm·数据库·python
k***92165 小时前
redis连接服务
数据库·redis·bootstrap
T-BARBARIANS5 小时前
mariadb galera集群在Openstack中的应用
数据库·负载均衡