MySQL游标
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了MySQL游标,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2582字,纯文字阅读大概需要4分钟。
内容图文
![MySQL游标](/upload/InfoBanner/zyjiaocheng/917/4730ea87e1e8462ea3b8c1a6d0b99038.jpg)
MySQL游标
While循环
-- MySQL游标只能用于存储过程和函数-- 使用过程 声明、打开游标、使用游标、关闭游标-- 如果未明确关闭游标,END时会自动关闭-- https://dev.mysql.com/doc/refman/8.0/en/error-handling.htmldrop 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();
内容总结
以上是互联网集市为您收集整理的MySQL游标全部内容,希望文章能够帮你解决MySQL游标所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。