MySQL 中的三中循环 while loop repeat 的基本用法
生活随笔
收集整理的這篇文章主要介紹了
MySQL 中的三中循环 while loop repeat 的基本用法
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
-- MySQL中的三中循環(huán) while 、 loop 、repeat 求 1-n 的和-- 第一種 while 循環(huán)
-- 求 1-n 的和
/* while循環(huán)語法:
while 條件 DO循環(huán)體;
end while;
*/
-- 實(shí)例:
create procedure sum1(a int)
begindeclare sum int default 0; -- default 是指定該變量的默認(rèn)值declare i int default 1;
while i<=a DO -- 循環(huán)開始set sum=sum+i;set i=i+1;
end while; -- 循環(huán)結(jié)束
select sum; -- 輸出結(jié)果
end
-- 執(zhí)行存儲(chǔ)過程
call sum1(100);
-- 刪除存儲(chǔ)過程
drop procedure if exists sum1-- 第二種 loop 循環(huán)
/*loop 循環(huán)語法:
loop_name:loopif 條件 THEN -- 滿足條件時(shí)離開循環(huán)leave loop_name; -- 和 break 差不多都是結(jié)束訓(xùn)話end if;
end loop;
*/-- 實(shí)例:
create procedure sum2(a int)
begindeclare sum int default 0;declare i int default 1;loop_name:loop -- 循環(huán)開始if i>a then leave loop_name; -- 判斷條件成立則結(jié)束循環(huán) 好比java中的 boeakend if;set sum=sum+i;set i=i+1;end loop; -- 循環(huán)結(jié)束select sum; -- 輸出結(jié)果
end
-- 執(zhí)行存儲(chǔ)過程
call sum2(100);
-- 刪除存儲(chǔ)過程
drop procedure if exists sum2-- 第三種 repeat 循環(huán)
/*repeat 循環(huán)語法
repeat循環(huán)體
until 條件 end repeat;
*/-- 實(shí)例;
create procedure sum3(a int)
begindeclare sum int default 0;declare i int default 1;repeat -- 循環(huán)開始set sum=sum+i;set i=i+1;until i>a end repeat; -- 循環(huán)結(jié)束select sum; -- 輸出結(jié)果
end-- 執(zhí)行存儲(chǔ)過程
call sum3(100);
-- 刪除存儲(chǔ)過程
drop procedure if exists sum3
?
存儲(chǔ)過程加游標(biāo)一起使用的基本操作
-- 存儲(chǔ)過程 加 游標(biāo)-- 建表 create table student( sid varchar(8) primary key, sname varchar(10), sex varchar(2), age int, classno varchar(6) ); -- 表中的數(shù)據(jù) insert into student values ('20170101','張石瑞','男','19','201701'), ('20170201','李佛','女','20','201702'), ('20170202','王法無','男','19','201702')/* 創(chuàng)建游標(biāo)和關(guān)閉游標(biāo)的四個(gè)步驟 -- 1、創(chuàng)建游標(biāo) (cur_name 游標(biāo)名字) declare cur_name cursor for select 語句; -- 2、打開游標(biāo) open cur_name; -- 3、提取游標(biāo)數(shù)據(jù) fetch cur_name [into 變量1,變量2,、、、、、]; -- 4、關(guān)閉(釋放)游標(biāo) close cur_name; */ -- 游標(biāo)的基本提取操作 create PROCEDURE proc1() BEGIN declare cur_sid varchar(20); declare cur_sname varchar(20); declare cur_sex varchar(20); declare cur_age varchar(20); declare cur_classno varchar(20); -- 1、 declare student_cur1 CURSOR for select sid,sname,sex,age,classno from student; -- 2、 open student_cur1; -- 3、 fetch student_cur1 into cur_sid,cur_sname,cur_sex,cur_age,cur_classno; select cur_sid,cur_sname,cur_sex,cur_age,cur_classno; -- 4、 close student_cur1; END-- 執(zhí)行存儲(chǔ)過程 call proc1(); -- 刪除存儲(chǔ)過程 drop procedure if exists proc1-- 游標(biāo)的循環(huán)遍歷‘ create PROCEDURE proc2() BEGIN declare cur_sid varchar(20); declare cur_sname varchar(20); declare cur_sex varchar(20); declare cur_age varchar(20); declare cur_classno varchar(20);declare sum int default 0; declare i int default 0; -- 1、 declare student_cur1 CURSOR for select sid,sname,sex,age,classno from student; -- 2、 open student_cur1; -- 3、 最簡(jiǎn)單的 while 遍歷方法 select count(sid) into sum from student; while i<sum DOfetch student_cur1 into cur_sid,cur_sname,cur_sex,cur_age,cur_classno;select cur_sid,cur_sname,cur_sex,cur_age,cur_classno;set i=i+1; end while; -- 4、 close student_cur1; END-- 執(zhí)行存儲(chǔ)過程 call proc2(); -- 刪除存儲(chǔ)過程 drop procedure if exists proc2-- 使用 loop 遍歷游標(biāo) create PROCEDURE proc3() BEGIN declare cur_sid varchar(20); declare cur_sname varchar(20); declare cur_sex varchar(20); declare cur_age varchar(20); declare cur_classno varchar(20);declare state int default false; -- 定義表示用于判斷游標(biāo)是否溢出 -- 1、 declare student_cur1 CURSOR for select sid,sname,sex,age,classno from student; -- 2、 open student_cur1; -- 3、 loop 遍歷游標(biāo) cur_loop:loop -- 循環(huán)開始 -- 循環(huán)開始的時(shí)候提取一次fetch student_cur1 into cur_sid,cur_sname,cur_sex,cur_age,cur_classno;select cur_sid,cur_sname,cur_sex,cur_age,cur_classno;if state thenleave cur_loop; end if; end loop; -- 循環(huán)結(jié)束 -- 4、 close student_cur1; END-- 執(zhí)行存儲(chǔ)過程 call proc3() -- 刪除存儲(chǔ)過程 drop procedure if exists proc3/* fetch是獲取游標(biāo)當(dāng)前指向的數(shù)據(jù)行,并將指針指向下一行,當(dāng)游標(biāo)已經(jīng)指向最后一行時(shí)繼續(xù)執(zhí)行會(huì)造成游標(biāo)溢出。 使用loop循環(huán)游標(biāo)時(shí),他本身是不會(huì)監(jiān)控是否到最后一條數(shù)據(jù)了,像下面代碼這種寫法,就會(huì)造成死循環(huán);read_loop:loop fetch cur into n,c; set total = total+c; end loop; 在MySql中,造成游標(biāo)溢出時(shí)會(huì)引發(fā)mysql預(yù)定義的NOT FOUND錯(cuò)誤,所以在上面使用下面的代碼指定了當(dāng)引發(fā)not found錯(cuò)誤時(shí)定義一個(gè)continue 的事件,指定這個(gè)事件發(fā)生時(shí)修改done變量的值。declare continue HANDLER for not found set done = true; 也有這樣寫的 DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' set done = true; 所以在循環(huán)時(shí)加上了下面這句代碼:--判斷游標(biāo)的循環(huán)是否結(jié)束 if done then leave read_loop; --跳出游標(biāo)循環(huán) end if; 如果done的值是true,就結(jié)束循環(huán)。繼續(xù)執(zhí)行下面的代碼。*/-- 性別翻轉(zhuǎn)create PROCEDURE proc4() BEGIN declare cur_sid int; declare cur_sex varchar(1);declare state int default false; -- 1、 declare sex_cur cursor for select sid,sex from student; -- declare CONTINUE HANDLER for not found set state=true; -- 2、 open sex_cur; -- 3、 sex_loop:LOOP -- 循環(huán)開始fetch sex_cur into cur_sid,cur_sex;if state thenleave sex_loop;end if;if cur_sex='男' thenupdate student set sex='女' where sid=cur_sid;end if;if cur_sex='女' thenupdate student set sex='男' where sid=cur_sid;end if; end loop; -- 循環(huán)結(jié)束 -- 4、 close sex_cur; END-- 執(zhí)行存儲(chǔ)過程 call proc4() -- 刪除存儲(chǔ)過程 drop procedure if exists proc4-- 表的查詢 select * from studenthttp://www.cnblogs.com/Lvhengshuai/category/997161.html
轉(zhuǎn)載于:https://www.cnblogs.com/Luouy/p/7301360.html
總結(jié)
以上是生活随笔為你收集整理的MySQL 中的三中循环 while loop repeat 的基本用法的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Linux命令【第一篇】
- 下一篇: 基于springmvc、ajax,后台连