存儲過程 認識 在一些編程語言中, 如pascal, 有一個概念叫"過程" procedure, 和"函數" function, 如VB中的sub. Java, Python, PHP, 沒有過程, 只有function. 過程(procedure) : 封裝了若幹條語句, 調用時, 這些封裝體執行 ...
存儲過程
認識
在一些編程語言中, 如pascal, 有一個概念叫"過程" procedure, 和"函數" function, 如VB中的sub. Java, Python, PHP, 沒有過程, 只有function.
過程(procedure) : 封裝了若幹條語句, 調用時, 這些封裝體執行.
函數(function): 是一個有返回值的 "過程" (ps: Python函數即可是過程, 也是是函數)
存儲過程(sql_procedure): 將若幹條sql封裝起來, 取一個名字, 即為過程, 把此過程存儲在資料庫中, 即存儲過程.
存儲過程-創建語法
-- 創建
create procedure procedureName()
begin
SQL語句1;
SQL語句2;.....
end
-- 調用
call procedureName();
第一存儲過程
helloWorld
-- 第一個存儲過程: 列印hello world
delimiter //
drop procedure if exists p1;
create procedure p1()
begin
select "hello world!";
select 1 + 1;
end //
delimiter ;
-- CALL 調用
call p1;
-- 查看: show procedure status;
show show procedure status;
效果
mysql> -- 第一個存儲過程: 列印hello world
delimiter //
drop procedure if exists p1;
create procedure p1()
begin
select "hello world!";
select 1 + 1;
end //
delimiter ;
Query OK, 0 rows affected (0.16 sec)
Query OK, 0 rows affected (0.16 sec)
mysql> call p1();
+--------------+
| hello world! |
+--------------+
| hello world! |
+--------------+
1 row in set (0.06 sec)
+-------+
| 1 + 1 |
+-------+
| 2 |
+-------+
1 row in set (0.21 sec)
Query OK, 0 rows affected (0.00 sec)
引入變數-declare 局部
存儲過程是可以編程的, 意味著可以用變數, 表達式, 控制結構來完成各種複雜的功能.
在存儲過程中, 用 declare 變數名 變數類型 [default 預設值].
-- 變數引入
drop procedure if exists p2;
delimiter //
create procedure p2()
begin
declare age int default 18;
declare height int default 180;
-- concat 拼接輸出
select concat("油哥的年齡是:", age, "身高是:", height);
end //
delimiter ;
call p2();
效果:
call p2();
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
+-------------------------------------------------+
| concat("油哥的年齡是:", age, "身高是:", height) |
+-------------------------------------------------+
| 油哥的年齡是:18身高是:180 |
+-------------------------------------------------+
1 row in set (0.10 sec)
Query OK, 0 rows affected (0.04 sec)
引入運算
在儲存過程中, 變數可以引入sql語句中合法的運算, 如 + - * /, 值的註意的是, 運算的結果,如何賦值給變數?
set 變數名 := expression 在存儲過程中的變數是一個局部變數.
set @變數名 := expression 用戶(會話)變數, 在存儲過程外面也能用, 類似"全局變數".
declare 變數名 變數類型 [default value] 用在過程中的局部變數, 聲明類型.
關於賦值 = 與 := 的區別
:=
- 標準的賦值符號, 在任何場景都是賦值.
=
- 只在 set 和 update 是和 := 一樣是賦值, 其他都是等於的作用.
drop procedure if exists p3;
delimiter //
create procedure p3()
begin
declare age int default 18;
select concat("現在的年齡是:", age);
-- 變數運算,賦值
set age := age + 10;
select concat("10年後, 年齡變成了:", age);
end //
delimiter ;
-- out
mysql> call p3();
+------------------------------+
| concat("現在的年齡是:", age) |
+------------------------------+
| 現在的年齡是:18 |
+------------------------------+
1 row in set (0.11 sec)
+------------------------------------+
| concat("10年後, 年齡變成了:", age) |
+------------------------------------+
| 10年後, 年齡變成了:28 |
+------------------------------------+
1 row in set (0.25 sec)
控制結構 if - then - else - end if;
-- 語法
if condition then
statement_01
else
statement_02
end if;
drop procedure if exists p4;
delimiter //
create procedure p4()
begin
declare age int default 18;
if age >= 18 then
select "已成年";
else
select "未成年";
end if;
end //
delimiter ;
-- test
call p4();
-- out
+--------+
| 已成年 |
+--------+
| 已成年 |
+--------+
1 row in set (0.09 sec)
Query OK, 0 rows affected (0.00 sec)
存儲過程傳參
存儲過程的括弧里, 可以聲明參數, 語法是 [in / out / inout] 參數名 參數類型
in 表示往procedure裡面傳參數; out 表示其往外發射參數
-- 輸入矩形的 width, height 求矩形的面積
drop procedure if exists p5;
delimiter //
create procedure p5(width int, height int)
begin
select concat("面積是:", width * height);
if width > height then
select "比較胖";
elseif width < height then
select "比較瘦";
else
select "方的一痞";
end if;
end //
delimiter ;
call p5(12, 13);
-- out
call p5(12, 13);
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.05 sec)
+-----------------------------------+
| concat("面積是:", width * height) |
+-----------------------------------+
| 面積是:156 |
+-----------------------------------+
1 row in set (0.11 sec)
+--------+
| 比較瘦 |
+--------+
| 比較瘦 |
+--------+
1 row in set (0.22 sec)
Query OK, 0 rows affected (0.01 sec)
流程式控制制 while , repeat, loop
任何編程語言, 只要具備控制結構順序, 選擇, 迴圈就足夠了.
感覺就是, 編程其實思路都是一樣的, 只是不同語言的應用場景, 語法特性有差別而已, 思路都是一樣的.
-- while 迴圈 語法
WHILE search_condition DO
statement_list
END WHILE [end_label]
-- 求 1+2+3+...100
drop procedure if exists p6;
delimiter //
create procedure p6()
begin
declare total int default 0;
declare num int default 0;
-- while 迴圈
while num <= 100 do
set total := total + num;
set num := num + 1;
end while;
-- 最後輸出結果
select concat("1+2+...100的值是: ", total) as 'sum';
end //
delimiter ;
call p6();
-- out
call p6();
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.05 sec)
+------------------------+
| sum |
+------------------------+
| 1+2+...100的值是: 5050 |
+------------------------+
1 row in set (0.09 sec)
改進: 求 1+2+....N 的和, 這裡引入參數 IN
-- 求 1+2+3+...N
drop procedure if exists p7;
delimiter //
-- 傳入參數 in類型
create procedure p7(in n int)
begin
declare total int default 0;
declare num int default 0;
-- while 迴圈
while num <= n do
set total := total + num;
set num := num + 1;
end while;
-- 最後輸出結果
select concat("1+2+.. 的值是: ", total) as 'sum';
end //
delimiter ;
call p7(10000);
-- out
call p7(10000);
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
+-------------------------+
| sum |
+-------------------------+
| 1+2+.. 的值是: 50005000 |
+-------------------------+
1 row in set (0.14 sec)
out 型參數
drop procedure if exists p8;
delimiter //
create procedure p8(in n int, out total int)
begin
-- 聲明一個局部(臨時)變數num來存儲 1..n
declare num int default 0;
-- while
while num <= n do
set total := total + num;
set num := num + 1;
end while;
-- select concat("the sum is:", total)
end //
delimiter ;
-- 區別: 沒有在 begin ..end 中聲明 total變數, 而是在 OUT類型的參數中.
-- out: 傳入一個變數去接收輸出
call p8(100, @cj);
mysql> select @cj;
+------+
| @cj |
+------+
| NULL |
+------+
1 row in set (0.10 sec)
NULL 的特殊性, 導致沒有正確輸出 , 解決: 給total 一個預設值即可
mysql> select null = null;
+-------------+
| null = null |
+-------------+
| NULL |
+-------------+
1 row in set (0.09 sec)
mysql> select 1 + null;
+----------+
| 1 + null |
+----------+
| NULL |
+----------+
1 row in set (0.05 sec)
-- 解決null的特殊性
drop procedure if exists p8;
delimiter //
create procedure p8(in n int, out total int)
begin
-- 先聲明一個局部(臨時)變數num來存儲 1..n
declare num int default 0;
-- 再給out變數一個預設值即可(順序是先declare哦)
set total := 0;
-- while
while num <= n do
set total := total + num;
set num := num + 1;
end while;
end //
delimiter ;
-- 區別: 沒有在 begin ..end 中聲明 total變數, 而是在 OUT類型的參數中.
-- out: 傳入一個變數去接收輸出的total變數值
call p8(100, @theSum);
select @theSum;
-- out
mysql> call p8(100, @theSum);
Query OK, 0 rows affected (0.00 sec)
mysql> select @theSum;
+---------+
| @theSum |
+---------+
| 5050 |
+---------+
1 row in set (0.11 sec)
小結參數 in 和 out 和 inout
- in 類型, 是要輸入一個值進去, 傳遞給procedure的in類型變數(傳值)
- out類型, 是要輸入一個變數進去, 接收procedure的out類型變數的值
- inout類型, 傳入值進入和傳入變數接收值出來
-- inout 類型
drop procedure if exists p9;
delimiter //
create procedure p9(inout age int)
begin
set age := age + 20;
end //
delimiter ;
-- call 的時候, inout, 首先要定義一個"全局(會話變數)", 然後再傳入
-- out
mysql> set @age := 100;
Query OK, 0 rows affected (0.00 sec)
mysql> call p9(@age);
Query OK, 0 rows affected (0.00 sec)
mysql> select @age;
+------+
| @age |
+------+
| 120 |
+------+