Stored Procedures
understanding
In some programming languages, such as pascal, there is a concept called "process" procedure, and "function" function, as in VB sub. Java, Python, PHP, there is no process, only function.
Procedure (Procedure) : encapsulates multiple statements, when invoked, perform these packages.
Function (function) : There is a return value of "process" (ps: Python function can be process, but also is a function)
Stored procedure (sql_procedure) : The sql encapsulates several pieces, a name, is the process , the process is stored in the database, i.e., the stored procedure .
Stored Procedures - Creating grammar
-- 创建
create procedure procedureName()
begin
SQL语句1;
SQL语句2;.....
end
-- 调用
call procedureName();
The first stored procedure
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;
effect
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)
The introduction of a local variable -declare
Stored procedures are programmable, meaning you can use variables, expressions, control structures to complete a variety of complex functions.
During storage, with declare variable type variable name [default 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();
effect:
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)
The introduction of operation
During storage, variables can be introduced into the sql statement lawful operations, such as + - * /, note that the value of that result of the operation, how to assign the variables?
set variable name: = expression variable stored in the process is a local variable.
set @ variable name: = expression user (session) variables can also be used outside of a stored procedure, similar to the "global variables."
declare variable type variable name [default value] for use in the process of local variables declared type.
= On assignment with: the difference =
:=
- Standard assignment symbols are assigned in any scenario.
=
- And update is only set and: = is the same as an assignment , the other is equal role.
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)
Control structure 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)
Stored procedure parameter passing
In parentheses stored procedure, you can declare the parameters, the syntax is [in / out / inout] parameter name parameter type
It indicates to the procedure in which transmission parameter ; out indicates a out its transmission parameters
-- 输入矩形的 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)
Process control while, repeat, loop
Any programming language, as long as the structure includes a control sequence, selection cycle is sufficient.
Feeling is, in fact, programming ideas are the same, but different application scenarios of language, syntax features differ only, the idea is the same.
-- 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)
Improvement: seeking 1 + 2 + .... N and, where the introduction of parameters 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 -type parameters
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 specificity, resulting in no output correctly resolve: to total a default value
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)
Summary parameters in and out and inout
- in type, to enter a value into, passed to the procedure in the type variable (by value)
- out type, is to enter a variable into account, the value of the variable out of the receiving procedure of the type
- inout type, and the incoming values into the variable reception value passed out
-- 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 |
+------+