您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

MySQL循环语句实例教程 mysql while循环测试

2025/12/1 21:47:16发布27次查看
在mysql数据库中操作同样有循环语句操作,标准的循环方式: while 循环 、 loop 循环和repeat循环。
还有一种非标准的循环: goto。 鉴于goto 语句的跳跃性会造成使用的的思维混乱,所以不建议使用。
这几个循环语句的格式如下:
while……do……end while
repeat……until end repeat
loop……end loop
goto。
目前我只测试了 while 循环:
delimiter $$ // 定义结束符为 $$
drop procedure if exists wk; // 删除 已有的 存储过程
create procedure wk() // 创建新的存储过程
begin
declare i int; // 变量声明
set i = 1;
while i
insert into user_profile (uid) values (i);
set i = i +1;
end while;
end $$ // 结束定义语句
// 调用
delimiter ; // 先把结束符 回复为;
call wk();
delimter : mysql 默认的 delimiter是; 告诉mysql解释器,该段命令是否已经结束了,mysql是否可以执行了。
这里使用 delimiter 重定义结束符的作用是: 不让存储过程中的语句在定义的时候输出。(脚本学堂www.jbxue.com)
创建 mysql 存储过程的简单语法为:
create procedure 存储过程名称( [in | out | inout] 参数 )
begin
mysql 语句
end
调用存储过程:
call 存储过程名称() // 名称后面要加()
二 、 repeat 循环
delimiter //drop procedure if exists looppc;
create procedure looppc()
begin
declare i int;
set i = 1;
repeat
insert into user_profile_company (uid) values (i+1);
set i = i + 1;
until i >= 20
end repeat;
---- 调用
call looppc()
三、 loop 循环
delimiter $$
drop procedure if exists lopp;
create procedure lopp()
begin
declare i int ;
set i = 1;
lp1 : loop // lp1 为循环体名称 loop 为关键字insert into user_profile (uid) values (i);set i = i+1;
if i > 30 then
leave lp1; // 离开循环体
end if;
end loop; // 结束循环
end $$

该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product