1、变量:在存储过程体中定义的局部变量和会话变量是不同的。在会话变量前面必须加@符号,且会话变量的作用域是整个会话,存储过程体可以使用declare语句来定义局部变量,存储过程的参数也被认为是局部变量,对于局部变量的使用不能在前面添加@符号。
在存储过程和存储函数中可以定义和使用变量。也就是在begin…end程序段中使用declare关键字,然后为变量进行赋值。
声明变量
declare xxx type default xxx
变量赋值
set xxx=sss;
或
select ... into xxx
2、流程控制语句
在存储过程或存储函数中使用流程控制语句进行执行。
①if语句
if search_condition then statement_list
elseif search_condition then statement_list
....
else statement_list
end if
②case语句
case case_value
when search_condition then statement_list
...
end case
③loop语句
[begin_lable] :loop
statement_list;
end loop [end_lable]
④leave跳出任何被标注的流程控制语句
⑤iterate跳出本次循环
⑥repeat带条件的循环语句
[begin_lable] repeat
statement_list
until search_condition
end repeat [end_lable]
⑦while语句,带条件的循环
[begin_label] while search_condition do
staetment_list
end while [end_lable]