ストアドプロシージャ
理解
Pascalのよういくつかのプログラミング言語では、VBのサブのように、「プロセス」手順、および「機能」機能と呼ばれる。ジャワやPython、PHPの概念があり、どのプロセス、唯一の機能はありません。
手順(手順):呼び出されたとき、複数の文をカプセル化し、これらのパッケージを実行します。
ファンクション(機能):「プロセス」の戻り値があります(PS:Pythonの関数はプロセスではなく、機能であることができます)
ストアドプロシージャ(sql_procedure):SQLは、いくつかの部分、名前をカプセル化しているプロセス、プロセスは、すなわち、データベースに格納されているストアドプロシージャを。
ストアドプロシージャ - 文法を作成します
-- 创建
create procedure procedureName()
begin
SQL语句1;
SQL语句2;.....
end
-- 调用
call procedureName();
最初のストアドプロシージャ
こんにちは世界
-- 第一个存储过程: 打印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
ストアドプロシージャは、あなたが使用できることを意味し、プログラム可能であり、変数、式、制御構造を複雑なさまざまな機能を完了させます。
貯蔵中の宣言変数の型変数名[既定のデフォルト]。
-- 变量引入
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文の合法的な業務に導入することができるような+ - * /、演算の結果の値は、どのように変数を代入することに注意してください?
変数名を設定します。=式のプロセスに格納された変数はローカル変数です。
変数名@設定:=式のユーザ(セッション)変数がと同様、ストアドプロシージャの外でも使用することができる「グローバル変数」。
変数の型変数名[既定値]を宣言するタイプを宣言したローカル変数のプロセスで使用するために。
=で代入:差異=
:=
- 標準割当シンボルは、任意のシナリオに割り当てられています。
=
- そして更新のみ設定されている:=は同じである割り当て、他は同じ役割。
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)
制御構造であれば - そして - 他の - 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)
ストアドプロシージャのパラメータの受け渡し
括弧のストアドプロシージャでは、あなたはパラメータを宣言することができ、構文は次のとおりです。パラメータ名パラメータタイプ[/アウト/ INOUT中]
これは、の手順を示した送信パラメータアウトが出て、その示し、送信パラメータを
-- 输入矩形的 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 循环 语法
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)
改善:+ 2 + 1 ... 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)
アウト型パラメータ
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特異性は、正しく解決:デフォルト値を合計します
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)
概要パラメータとアウトとINOUT
- するタイプに値を入力中に、(値によって)型変数の手順に渡されます
- タイプから、することがある変数を入力タイプの受信手順のうち、変数の値は、考慮に
- 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 |
+------+