人妖在线一区,国产日韩欧美一区二区综合在线,国产啪精品视频网站免费,欧美内射深插日本少妇

新聞動(dòng)態(tài)

MySQL存儲(chǔ)過(guò)程概念、原理與常見用法詳解

發(fā)布日期:2022-03-10 12:15 | 文章來(lái)源:站長(zhǎng)之家

本文實(shí)例講述了MySQL存儲(chǔ)過(guò)程概念、原理與常見用法。分享給大家供大家參考,具體如下:

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ì)有所幫助。

香港服務(wù)器租用

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

相關(guān)文章

實(shí)時(shí)開通

自選配置、實(shí)時(shí)開通

免備案

全球線路精選!

全天候客戶服務(wù)

7x24全年不間斷在線

專屬顧問(wèn)服務(wù)

1對(duì)1客戶咨詢顧問(wèn)

在線
客服

在線客服:7*24小時(shí)在線

客服
熱線

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

關(guān)注
微信

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