1. 程式人生 > 資料庫 >MySQL儲存過程概念、原理與常見用法詳解

MySQL儲存過程概念、原理與常見用法詳解

本文例項講述了MySQL儲存過程概念、原理與常見用法。分享給大家供大家參考,具體如下:

1、儲存過程的概念

在一些語言中,如pascal,有一個概念叫“過程”procedure,和“函式”function,在php中,沒有過程,只有函式。

過程:封裝了若干條語句,呼叫時,這些封裝體執行
函式:是一個有返回值的“過程”
總結:過程是一個沒有返回值的函式

在MySQL中:

我們把若干條sql封裝起來,起個名字 —— 過程
把此過程儲存在資料庫中 —— 儲存過程

2、建立儲存過程

create procedure procedureName()
begin
  //--sql 語句
end$

3、檢視已有的儲存過程

show procedure status

4、刪除儲存過程

drop procedure procedureName;

5、呼叫儲存過程

call procedureName();

6、第一個儲存過程

注意:我這裡已經將MySQL的結束識別符號改為$,如果要知道怎麼設定為$,請參考我的另一篇文章:MySQL觸發器。

create procedure p1()
begin
  select 2+3;
end$

呼叫:

call p1();

顯示結果:

這裡寫圖片描述

7、引入變數

儲存過程是可以程式設計的,意味著可以使用變數,表示式,控制結構來完成複雜的功能,在儲存過程中,用declare宣告變數:

declare 變數名 變數型別 [default 預設值]

使用:

create procedure p2()
begin
  declare age int default 18;
  declare height int default 180;
  select concat('年齡:',age,'身高:',height);
end$

顯示結果:

這裡寫圖片描述

8、引入表示式

儲存過程中,變數可以在sql語句中進行合法的運算,如+-*/。變數的賦值形式:

set 變數名:= expression

使用:

create procedure p3()
begin
  declare age int default 18;
  set age := age + 20;
  select concat('20年後年齡:',age);
end$

顯示結果:

這裡寫圖片描述

9、引入選擇控制結構

格式:

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$

顯示結果:

這裡寫圖片描述

10、給儲存過程傳參

在定義儲存過程的括號中,可以宣告引數,語法:

[in/out/inout] 引數名 引數型別

使用:

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$

顯示結果:

這裡寫圖片描述

11、使用while迴圈結構

需求:從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$

顯示結果:

這裡寫圖片描述

12、儲存過程引數的輸入輸出型別

主要有in、out、inout三種類型
需求:從1加到N
輸入型的資料是我們給出值,輸出型是我們給出變數名,用於乘裝輸出的變數值。

(1)in型,此時in為輸入行引數,它能接受到我們的輸入

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$

呼叫:

call p7(100);

輸出結果:

這裡寫圖片描述

(2)out型別的引數

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$

呼叫:

call p8(100,@total); --100為輸入型引數,而@total為輸出型變數
select @total; --輸出@total變數

輸出結果:

這裡寫圖片描述

(3)inout型別的引數

create procedure p9(inout age int)
begin
  set age := age+20;
end$

呼叫:

set @age = 18; --設定@age變數為18
call p9(@age); --呼叫p9儲存過程,@age變數為實參
select @age; --顯示@age變數

輸出結果:

這裡寫圖片描述

inout型變數的實參也是一個變數名,這個變數在儲存過程中既作為輸入變數,又作為輸出變數。

13、case結構的用法

使用:

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$

輸出結果:

這裡寫圖片描述

14、repeat迴圈結構

格式:

[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$

輸出結果:

這裡寫圖片描述

更多關於MySQL相關內容感興趣的讀者可檢視本站專題:《MySQL儲存過程技巧大全》、《MySQL常用函式大彙總》、《MySQL日誌操作技巧大全》、《MySQL事務操作技巧彙總》及《MySQL資料庫鎖相關技巧彙總》

希望本文所述對大家MySQL資料庫計有所幫助。