mysql while,loop,repeat循环,符合条件跳出循环,游标
DROP PROCEDURE IF EXISTS mywhile;
CREATE PROCEDURE mywhile ()
BEGIN
DECLARE i INT default 0;
outer_label: BEGIN #设置一个标记并设置开始
while i<10 DO
SET i = i + 1;
IF i > 2 THEN
select '跳出循环' column1;
LEAVE outer_label; #满足条件,终止循环,跳转到end outer_label标记
ELSE
SELECT i as column1;
END IF;
end while;
END outer_label; #设置标记并结束
END
call mywhile;
DROP PROCEDURE IF EXISTS myloop;
CREATE PROCEDURE myloop ()BEGIN
DECLARE i INT default 0;
loop_label:LOOP#循环开始
SET i = i + 1;
IF i > 3 THEN
select '跳出循环' column1;
LEAVE loop_label; #终止循环
ELSE
SELECT NOW() as column1;
END IF;
END LOOP;
ENd
call myloop;
DROP PROCEDURE IF EXISTS myrepeat;
CREATE PROCEDURE myrepeat ()
BEGIN
DECLARE i INT default 0;
REPEAT
SET i = i + 1;
until i>2
end REPEAT;
END
call myrepeat;
DROP PROCEDURE IF EXISTS myCURSOR;
CREATE PROCEDURE myCURSOR ()
BEGIN
DECLARE user_name VARCHAR(32);
/* 声明游标 */
DECLARE my_cursor CURSOR FOR select nickname from users;
/* 打开游标 */
OPEN my_cursor;
sale_loop: LOOP
FETCH my_cursor INTO user_name;
select user_name;
END LOOP;
-- 关闭游标
close my_cursor;
end
call myCURSOR;
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!