五月综合激情婷婷六月,日韩欧美国产一区不卡,他扒开我内裤强吻我下面视频 ,无套内射无矿码免费看黄,天天躁,日日躁,狠狠躁

新聞動態(tài)

MySQL存儲過程之流程控制while,repeat,loop循環(huán)

發(fā)布日期:2022-07-15 19:21 | 文章來源:腳本之家

  • 循環(huán)是一段在程序中只出現(xiàn)一次,但可能會連續(xù)運行多次的代碼。
  • 循環(huán)中的代碼會運行特定的次數(shù),或者是運行到特定條件成立時結(jié)束循環(huán)。

循環(huán)分類:

  • while
  • repeat
  • loop

循環(huán)控制:

leave類似于break,跳出,結(jié)束當(dāng)前所在的循環(huán)

iterate類似于continue,繼續(xù),結(jié)束本次循環(huán),繼續(xù)下一次

while循環(huán)

【標(biāo)簽:】while 循環(huán)條件 do
循環(huán)體;
end while【 標(biāo)簽】;
-- 創(chuàng)建測試表
create table user (
uid int primary_key,
username varchar ( 50 ),
password varchar ( 50 )
);
-- -------存儲過程-while
delimiter $$
create procedure proc16_while1(in insertcount int)
begin
declare i int default 1;
label:while i<=insertcount do
insert into user(uid,username,`password`) values(i,concat('user-',i),'123456');
set i=i+1;
end while label;
end $$
delimiter ;
call proc16_while(10);

存儲過程語法是固定的:delimiter $$ create peocedure 循環(huán)名(參數(shù))begin 代碼 end $$ delimiter;

注意在寫循環(huán)體的時候,必須要要有定義循環(huán)的初識變量,采用declare i int default 默認(rèn)值

然后就是dlabel:while 判斷條件 do循環(huán)體end while label;end && 必須要有

-- -------存儲過程-while + leave
truncate table user;
delimiter $$
create procedure proc16_while2(in insertcount int)
begin
declare i int default 1;
label:while i<=insertcount do
insert into user(uid,username,`password`) values(i,concat('user-',i),'123456');
if i=5 then leave label;
end if;
set i=i+1;
end while label;
end $$
delimiter ;
call proc16_while2(10);

如果在內(nèi)部需要跳出循環(huán)的話,采用if 判斷 ,但是最后需要end if 結(jié)尾

這里的leave就是 跳出循環(huán),相對于break

-- -------存儲過程-while+iterate
truncate table user;
delimiter $$
create procedure proc16_while3(in insertcount int)
begin
declare i int default 1;
label:while i<=insertcount do
set i=i+1;
if i=5 then iterate label;
end if;
insert into user(uid,username,`password`) values(i,concat('user-',i),'123456');
end while label;
end $$
delimiter ;
call proc16_while3(10);

這里的iterate 相對于continue 遇到就不執(zhí)行下面的代碼

repeat循環(huán)

[標(biāo)簽:]repeat
循環(huán)體;
until 條件表達(dá)式
end repeat [標(biāo)簽];
-- -------存儲過程-循環(huán)控制-repeat
use mysql7_procedure;
truncate table user;
delimiter $$
create procedure proc18_repeat(in insertCount int)
begin
declare i int default 1;
label:repeat
insert into user(uid, username, password) values(i,concat('user-',i),'123456');
set i = i + 1;
until i > insertCount
end repeat label;
select '循環(huán)結(jié)束';
end $$
delimiter ;
call proc18_repeat(100);

這個相對于是,無論如何都會執(zhí)行一次的循環(huán),然后是在內(nèi)部進(jìn)行判斷,如果滿足了就直接跳出

loop循環(huán)

[標(biāo)簽:] loop
循環(huán)體;
if 條件表達(dá)式 then
leave [標(biāo)簽];
end if;
end loop;
-- -------存儲過程-循環(huán)控制-loop
truncate table user;
delimiter $$
create procedure proc19_loop(in insertCount int)
begin
declare i int default 1;
label:loop
insert into user(uid, username, password) values(i,concat('user-',i),'123456');
set i = i + 1;
if i > 5
then
leave label;
end if;
end loop label;
select '循環(huán)結(jié)束';
end $$
delimiter ;
call proc19_loop(10);

這個和repeat不同的是,需要執(zhí)行之后,利用leave 跳出循環(huán),無論是使用哪種都可以達(dá)到我們需要的效果,但是在業(yè)務(wù)中的應(yīng)用場景,while還是相對比較的多。

到此這篇關(guān)于MySQL存儲過程之流程控制while,repeat,loop循環(huán)的文章就介紹到這了,更多相關(guān)MySQL存儲過程 內(nèi)容請搜索本站以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持本站!

國外穩(wěn)定服務(wù)器

版權(quán)聲明:本站文章來源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非maisonbaluchon.cn所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請聯(lián)系alex-e#qq.com處理。

實時開通

自選配置、實時開通

免備案

全球線路精選!

全天候客戶服務(wù)

7x24全年不間斷在線

專屬顧問服務(wù)

1對1客戶咨詢顧問

在線
客服

在線客服:7*24小時在線

客服
熱線

400-630-3752
7*24小時客服服務(wù)熱線

關(guān)注
微信

關(guān)注官方微信
頂部