本文實(shí)例講述了MySQL存儲(chǔ)過(guò)程概念、原理與常見(jiàn)用法。分享給大家供大家參考,具體如下:
1、存儲(chǔ)過(guò)程的概念
在一些語(yǔ)言中,如pascal,有一個(gè)概念叫“過(guò)程”procedure,和“函數(shù)”function,在php中,沒(méi)有過(guò)程,只有函數(shù)。
過(guò)程:封裝了若干條語(yǔ)句,調(diào)用時(shí),這些封裝體執(zhí)行
函數(shù):是一個(gè)有返回值的“過(guò)程”
總結(jié):過(guò)程是一個(gè)沒(méi)有返回值的函數(shù)
在MySQL中:
我們把若干條sql封裝起來(lái),起個(gè)名字 —— 過(guò)程
把此過(guò)程存儲(chǔ)在數(shù)據(jù)庫(kù)中 —— 存儲(chǔ)過(guò)程
2、創(chuàng)建存儲(chǔ)過(guò)程
create procedure procedureName()
begin
//--sql 語(yǔ)句
end$
3、查看已有的存儲(chǔ)過(guò)程
4、刪除存儲(chǔ)過(guò)程
drop procedure procedureName;
5、調(diào)用存儲(chǔ)過(guò)程
6、第一個(gè)存儲(chǔ)過(guò)程
注意:我這里已經(jīng)將MySQL的結(jié)束標(biāo)識(shí)符改為$,如果要知道怎么設(shè)置為$,請(qǐng)參考我的另一篇文章:MySQL觸發(fā)器。
create procedure p1()
begin
select 2+3;
end$
調(diào)用:
顯示結(jié)果:

7、引入變量
存儲(chǔ)過(guò)程是可以編程的,意味著可以使用變量,表達(dá)式,控制結(jié)構(gòu)來(lái)完成復(fù)雜的功能,在存儲(chǔ)過(guò)程中,用declare聲明變量:
declare 變量名 變量類(lèi)型 [default 默認(rèn)值]
使用:
create procedure p2()
begin
declare age int default 18;
declare height int default 180;
select concat('年齡:',age,'身高:',height);
end$
顯示結(jié)果:

8、引入表達(dá)式
存儲(chǔ)過(guò)程中,變量可以在sql語(yǔ)句中進(jìn)行合法的運(yùn)算,如+-*/。變量的賦值形式:
set 變量名:= expression
使用:
create procedure p3()
begin
declare age int default 18;
set age := age + 20;
select concat('20年后年齡:',age);
end$
顯示結(jié)果:

9、引入選擇控制結(jié)構(gòu)
格式:
if condition then
statement
elseif
statement
else
statement
end if;
使用:
create procedure p4()
begin
declare age int default 18;
if age >= 18 then
select '已成年';
else
select '未成年';
end if;
end$
顯示結(jié)果:

10、給存儲(chǔ)過(guò)程傳參
在定義存儲(chǔ)過(guò)程的括號(hào)中,可以聲明參數(shù),語(yǔ)法:
[in/out/inout] 參數(shù)名 參數(shù)類(lèi)型
使用:
create procedure p5(width int,height int)
begin
select concat('你的面積是:',width * height) as area;
if width > height then
select '你比較胖';
elseif width height then
select '你比較瘦';
else
select '你比較方';
end if;
end$
顯示結(jié)果:

11、使用while循環(huán)結(jié)構(gòu)
需求:從1加到100
使用:
create procedure p6()
begin
declare total int default 0;
declare num int default 0;
while num = 100 do
set total := total + num;
set num := num + 1;
end while;
select total;
end$
顯示結(jié)果:

12、存儲(chǔ)過(guò)程參數(shù)的輸入輸出類(lèi)型
主要有in、out、inout三種類(lèi)型
需求:從1加到N
輸入型的數(shù)據(jù)是我們給出值,輸出型是我們給出變量名,用于乘裝輸出的變量值。
(1)in型,此時(shí)in為輸入行參數(shù),它能接受到我們的輸入
create procedure p7(in n int)
begin
declare total int default 0;
declare num int default 0;
while num = n do
set total := total + num;
set num := num + 1;
end while;
select total;
end$
調(diào)用:
輸出結(jié)果:

(2)out類(lèi)型的參數(shù)
create procedure p8(in n int,out total int)
begin
declare num int default 0;
set total := 0;
while num = n do
set total := total + num;
set num := num + 1;
end while;
end$
調(diào)用:
call p8(100,@total); --100為輸入型參數(shù),而@total為輸出型變量
select @total; --輸出@total變量
輸出結(jié)果:

(3)inout類(lèi)型的參數(shù)
create procedure p9(inout age int)
begin
set age := age+20;
end$
調(diào)用:
set @age = 18; --設(shè)置@age變量為18
call p9(@age); --調(diào)用p9存儲(chǔ)過(guò)程,@age變量為實(shí)參
select @age; --顯示@age變量
輸出結(jié)果:

inout型變量的實(shí)參也是一個(gè)變量名,這個(gè)變量在存儲(chǔ)過(guò)程中既作為輸入變量,又作為輸出變量。
13、case結(jié)構(gòu)的用法
使用:
create procedure p10()
begin
declare pos int default 0;
set pos := floor(5*rand());
case pos
when 1 then select '仍然在飛';
when 2 then select '落在海里';
when 3 then select '落在陸上';
else select '我不知道在哪里';
end case;
end$
輸出結(jié)果:

14、repeat循環(huán)結(jié)構(gòu)
格式:
[begin_label:] REPEAT
statement_list
UNTIL search_condition
END REPEAT [end_label]
需求:從1加到100
create procedure p11()
begin
declare total int default 0;
declare num int default 0;
r:repeat
set total:= total + num;
set num:=num + 1;
until num > 100
end repeat r;
select total;
end$
輸出結(jié)果:

更多關(guān)于MySQL相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《MySQL存儲(chǔ)過(guò)程技巧大全》、《MySQL常用函數(shù)大匯總》、《MySQL日志操作技巧大全》、《MySQL事務(wù)操作技巧匯總》及《MySQL數(shù)據(jù)庫(kù)鎖相關(guān)技巧匯總》
希望本文所述對(duì)大家MySQL數(shù)據(jù)庫(kù)計(jì)有所幫助。
您可能感興趣的文章:- mysql 存儲(chǔ)過(guò)程中變量的定義與賦值操作
- mysql存儲(chǔ)過(guò)程 游標(biāo) 循環(huán)使用介紹
- MySQL存儲(chǔ)過(guò)程使用實(shí)例詳解
- MySQL存儲(chǔ)過(guò)程例子(包含事務(wù),輸出參數(shù),嵌套調(diào)用)
- MySql存儲(chǔ)過(guò)程與函數(shù)詳解
- mysql 查詢(xún)數(shù)據(jù)庫(kù)中的存儲(chǔ)過(guò)程與函數(shù)的語(yǔ)句
- mysql 導(dǎo)入導(dǎo)出數(shù)據(jù)庫(kù)以及函數(shù)、存儲(chǔ)過(guò)程的介紹
- MySQL 有輸入輸出參數(shù)的存儲(chǔ)過(guò)程實(shí)例
- MySQL 存儲(chǔ)過(guò)程中執(zhí)行動(dòng)態(tài)SQL語(yǔ)句的方法
- Mysql存儲(chǔ)過(guò)程和函數(shù)區(qū)別介紹
- MYSQL的存儲(chǔ)過(guò)程和函數(shù)簡(jiǎn)單寫(xiě)法
- MySQL存儲(chǔ)過(guò)程中游標(biāo)循環(huán)的跳出和繼續(xù)操作示例