MySQL游标

本文深入探讨了MySQL中游标的使用方法,包括声明、打开、使用和关闭游标的过程。并通过三种不同的循环结构(While、Repeat、Loop)演示了如何在存储过程和函数中利用游标遍历数据,同时介绍了如何将检索到的数据插入到新的表中。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

MySQL游标

While循环

-- MySQL游标只能用于存储过程和函数
-- 使用过程 声明、打开游标、使用游标、关闭游标
-- 如果未明确关闭游标,END时会自动关闭
-- https://dev.mysql.com/doc/refman/8.0/en/error-handling.html
drop PROCEDURE if exists process_user;
create procedure process_user()
begin
	-- Declare local variables
	declare done boolean default true;
	declare user_id int;
	declare user_info varchar(50);
	
	-- Declare the cursor
	declare cursor_user cursor for select id, user_name from sys_user;
	
	-- Declare continue handler
	declare continue handler for sqlstate '02000' set done=false;
	
	-- Create a table to store the results;
	create table if not exists process_user_result(user_id int, user_name varchar(50));
	
	-- Open the cursor
	open cursor_user;
	
	-- Repeat through all rows
	repeat
		fetch cursor_user into user_id, user_info;
		if done then 
			insert into process_user_result(user_id, user_name) 
			values(user_id, user_info);
		end if;
	until (not done) end repeat;
	
	-- Close the cursor
	close cursor_user;
end;

call process_user();

Repeat循环

drop PROCEDURE if exists process_user;
create procedure process_user()
begin
	-- Declare local variables
	declare done boolean default true;
	declare user_id int;
	declare user_info varchar(50);
	
	-- Declare the cursor
	declare cursor_user cursor for select id, user_name from sys_user;
	
	-- Declare continue handler
	declare continue handler for not found set done=false;
	
	-- Create a table to store the results;
	create table if not exists process_user_result(user_id int, user_name varchar(50));
	
	-- Open the cursor
	open cursor_user;
	
	-- Loop through all rows
	fetch cursor_user into user_id, user_info;
	while done do
		insert into process_user_result(user_id, user_name) 
		values(user_id, user_info); 
		fetch cursor_user into user_id, user_info;
	end while;
	
	-- Close the cursor
	close cursor_user;
end;

call process_user();

Loop循环

drop PROCEDURE if exists process_user;
create procedure process_user()
begin
	-- Declare local variables
	declare done boolean default true;
	declare user_id int;
	declare user_info varchar(50);
	
	-- Declare the cursor
	declare cursor_user cursor for select id, user_name from sys_user;
	
	-- Declare continue handler
	declare continue handler for not found set done=false;
	
	-- Create a table to store the results;
	create table if not exists process_user_result(user_id int, user_name varchar(50));
	
	-- Open the cursor
	open cursor_user;
	
	-- Loop through all rows
	read_loop:loop
		fetch cursor_user into user_id, user_info;
		if not done then
			leave read_loop;
		end if;
		insert into process_user_result(user_id, user_name) values(user_id, user_info);
	end loop;
	
	-- Close the cursor
	close cursor_user;
end;

call process_user();
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值