Stored procedure
advantage:
- high speed
- Secure the database
- easy to use
Create a stored procedure:
create procedure 存储过程名()
begin
sql语句
end;
transfer:
call 存储过程名();
variable:
Variables are divided into local variables and session variables
Local variables
Define variables:
declare 变量名 数据类型 [default 默认值];
Assignment:
set 局部变量 = 值;
select 值 into 局部变量;
Inquire:
select 局部变量名;
Session variables
Define variables start with @ and can be directly defined by set:
set @x = 1;
Process control
Flow control statements can be divided into selection structure and tame structure
Select structure
if 条件表达式
then sql语句;
[else
sql语句;]
end if;
Cyclic structure
[标志名]:while 条件表达式 do
sql语句
end while [标志名];
注:
leave 循环标志名 强制退出循环
iterate 结束本次循环,回到循环开始处,进行下一次循环