MySQL存儲(chǔ)過(guò)程概念、原理與常見(jiàn)用法詳解
本文實(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ò)程
show procedure status
4、刪除存儲(chǔ)過(guò)程
drop procedure procedureName;
5、調(diào)用存儲(chǔ)過(guò)程
call procedureName();
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)用:
call p1();
顯示結(jié)果:
7、引入變量
存儲(chǔ)過(guò)程是可以編程的,意味著可以使用變量,表達(dá)式,控制結(jié)構(gòu)來(lái)完成復(fù)雜的功能,在存儲(chǔ)過(guò)程中,用declare聲明變量:
declare 變量名 變量類型 [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ù)類型
使用:
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ù)的輸入輸出類型
主要有in、out、inout三種類型
需求:從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)用:
call p7(100);
輸出結(jié)果:
(2)out類型的參數(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類型的參數(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)容感興趣的讀者可查看本站專題:《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 查詢數(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ù)操作示例
相關(guān)文章
MySql中把一個(gè)表的數(shù)據(jù)插入到另一個(gè)表中的實(shí)現(xiàn)代碼
本篇文章是對(duì)MySql中把一個(gè)表的數(shù)據(jù)插入到另一個(gè)表中的實(shí)現(xiàn)代碼進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05MySQL 去除重復(fù)數(shù)據(jù)實(shí)例詳解
這篇文章主要介紹了MySQL 去除重復(fù)數(shù)據(jù)實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-06-06Mysql數(shù)據(jù)庫(kù)按時(shí)間點(diǎn)恢復(fù)實(shí)戰(zhàn)記錄
如果客戶在某時(shí)間節(jié)點(diǎn)由于誤操作,導(dǎo)致數(shù)據(jù)丟失,RDS管控服務(wù)是如何進(jìn)行恢復(fù)的呢?通過(guò)Mysql數(shù)據(jù)庫(kù)按時(shí)間點(diǎn)恢復(fù)該如何操作呢,感興趣的朋友跟隨小編一起看看吧2021-06-06從其他電腦訪問(wèn)本機(jī)的Mysql的設(shè)置方法
如果需要讓特定的用戶從給定域(例如mydomain.com)的所有計(jì)算機(jī)上訪問(wèn) MySQL 服務(wù)器,你可以執(zhí)行在賬戶名的 host 部分使用了通配符“%” 的 GRANT 語(yǔ)句2008-11-11面試中老生常談的MySQL問(wèn)答集錦夯實(shí)基礎(chǔ)
這篇文章主要為大家介紹了面試中老生常談的MySQL問(wèn)答集錦,不僅可以幫助大家順利通過(guò)面試更可以夯實(shí)大家的基礎(chǔ),有需要的朋友可以借鑒參考下2022-03-03