Stored procedure & variable & process control

Stored procedure

advantage:

  1. high speed
  2. Secure the database
  3. 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 结束本次循环,回到循环开始处,进行下一次循环

Guess you like

Origin blog.csdn.net/weixin_45936162/article/details/106599420