存储过程语法
存储过程是可以编程的,意味着可以使用变量,表达式,控制结构 ,来完成比较复杂的功能。
变量
DECLARE
通过 DECLARE 可以定义一个局部变量,该变量的作用范围只能在 BEGIN…END 块中。
语法:
DECLARE var_name[,...] type [DEFAULT value]解释:
var_name:变量名称
type:MySQL支持的任何数据类型
[DEFAULT value]:可选项,变量指定默认值。一次可以定义多个同类型的变量,各变量名称之间以逗号隔开。
示例 :
delimiter $
create procedure pro_test2()
begin
declare num int default 5;
select num + 10;
end$
delimiter ;SET
直接赋值使用 SET,可以赋常量或者赋表达式
语法:
SET var_name = expr [, var_name = expr] ...解释:
var_name:变量的名称
expr:赋值表达式;一个set语句可以同时为多个变量赋值,各个变量的赋值语句之间用逗号隔开
示例 :
DELIMITER $
CREATE PROCEDURE pro_test3()
BEGIN
DECLARE NAME VARCHAR(20);
SET NAME = 'BOTOY';
SELECT NAME ;
END$
DELIMITER ;select ... into
赋值操作
示例:
DELIMITER $
CREATE PROCEDURE pro_test4()
BEGIN
declare num int;
select count(*) into num from bgs_merchant;
select num;
END$
DELIMITER ;传递参数
语法格式 :
create procedure procedure_name([in/out/inout] 参数名 参数类型)
...
IN : 该参数可以作为输入,也就是需要调用方传入值 , 默认
OUT: 该参数作为输出,也就是该参数可以作为返回值
INOUT: 既可以作为输入参数,也可以作为输出参数IN - 输入
示例 :
delimiter $
alter procedure pro_test6(in height int)
begin
declare description varchar(50) default '';
if height >= 180 then
set description='超预期';
elseif height >= 170 and height < 180 then
set description='标准';
else
set description='一般';
end if;
select concat('身高:', height , ',身材类型:',description);
end$
delimiterOUT-输出
示例:
delimiter $
create procedure pro_test7(in height int , out description varchar(100))
begin
if height >= 180 then
set description='超预期';
elseif height >= 170 and height < 180 then
set description='标准';
else
set description='一般';
end if;
end$
delimiter调用:
call pro_test7(188, @description)$
select @description$- TIPS
- @description
这种变量要在变量名称前面加上“@”符号,叫做用户会话变量,代表整个会话过程他都是有作用的,这个类似于全局变量一样。
- @@global.sort_buffer_size
这种在变量前加上 "@@" 符号, 叫做 系统变量
循环判断
if条件判断
语法 :
if search_condition then statement_list
[elseif search_condition then statement_list] ...
[else statement_list]
end if;示例 :
delimiter $
create procedure pro_test5()
begin
declare height int default 175;
declare description varchar(50);
if height >= 180 then
set description = '超预期';
elseif height >= 170 and height < 180 then
set description = '标准';
else
set description = '一般';
end if;
select description ;
end$
delimiter ;case结构
语法
-- 方式一 :
CASE case_value
WHEN when_value THEN statement_list
[WHEN when_value THEN statement_list] ...
[ELSE statement_list]
END CASE;
-- 方式二 :
CASE
WHEN search_condition THEN statement_list
[WHEN search_condition THEN statement_list] ...
[ELSE statement_list]
END CASE;示例 :
-- 方式一:
delimiter $
create procedure pro_test8(sex int)
begin
declare result varchar(10);
case sex
when 0 then set result = '女';
when 1 then set result = '男';
else set result = '中';
end case;
select concat('性别:',result) as content;
end$
delimiter;
-- 方式二:
delimiter $
create procedure pro_test9(month int)
begin
declare result varchar(20);
case
when month >= 1 and month <=3 then
set result = '第一季度';
when month >= 4 and month <=6 then
set result = '第二季度';
when month >= 7 and month <=9 then
set result = '第三季度';
when month >= 10 and month <=12 then
set result = '第四季度';
end case;
select concat('输入月份 :', month , ' , 该月份为 : ' , result) as content ;
end$
delimiter ;while循环
满足条件才执行
语法
while search_condition do
statement_list
end while;示例
delimiter $
create procedure pro_test10(n int)
begin
declare total int default 0;
declare num int default 1;
while num<=n do
set total = total + num;
set num = num + 1;
end while;
select total;
end$
delimiter ;repeat结构
满足条件退出循环
语法 :
REPEAT
statement_list
UNTIL search_condition
END REPEAT;示例 :
delimiter $
create procedure pro_test11(n int)
begin
declare total int default 0;
repeat
set total = total + n;
set n = n - 1;
until n=0
end repeat;
select total ;
end$
delimiter ;loop语句
LOOP 实现简单的循环,退出循环的条件需要使用其他的语句定义,通常可以使用 LEAVE 语句实现
语法:
[begin_label:] LOOP
statement_list
END LOOP [end_label]如果不在 statement_list 中增加退出循环的语句,那么 LOOP 语句可以用来实现简单的死循环。
leave语句
用来从标注的流程构造中退出,通常和 BEGIN ... END 或者循环一起使用。
示例:
delimiter $
CREATE PROCEDURE pro_test11(n int)
BEGIN
declare total int default 0;
ins: LOOP
IF n <= 0 then
leave ins;
END IF;
set total = total + n;
set n = n - 1;
END LOOP ins;
select total;
END$
delimiter ;游标/光标
游标是用来存储查询结果集的数据类型 , 在存储过程和函数中可以使用光标对结果集进行循环的处理。光标的使用包括光标的声明、OPEN、FETCH 和 CLOSE。
语法:声明光标
DECLARE cursor_name CURSOR FOR select_statement ;语法:OPEN 光标
OPEN cursor_name ;语法:FETCH 光标
FETCH cursor_name INTO var_name [, var_name] ...语法:CLOSE 光标
CLOSE cursor_name ;示例 :
查询bgs_merchant表中数据, 并逐行获取进行展示
delimiter $
create procedure pro_test13()
begin
declare mch_id BIGINT(11);
declare mch_name varchar(50);
declare m_result cursor for select merchant_id,merchant_name from bgs_merchant;
open m_result;
fetch m_result into mch_id,mch_name;
select concat('merchantId=',mch_id , ', merchantName=',mch_name);
fetch m_result into mch_id,mch_name;
select concat('merchantId=',mch_id , ', merchantName=',mch_name);
fetch m_result into mch_id,mch_name;
select concat('merchantId=',mch_id , ', merchantName=',mch_name);
close m_result;
end$
delimiter;通过循环结构 , 获取游标中的数据 :
create procedure pro_test14()
begin
declare mch_id BIGINT(11);
declare mch_name varchar(50);
DECLARE has_data int default 1;
DECLARE m_result CURSOR FOR select merchant_id,merchant_name from bgs_merchant;
DECLARE EXIT HANDLER FOR NOT FOUND set has_data = 0;
open m_result;
repeat
fetch m_result into mch_id , mch_name;
select concat('merchantId=',mch_id, ', merchantName=' ,mch_name);
until has_data = 0
end repeat;
close m_result;
end$
delimiter ;注意:
游标select的字段数需要与fetch into的变量数一致
存储函数
语法结构:
CREATE FUNCTION function_name([param type ... ])
RETURNS type
BEGIN
...
END;示例 :
delimiter $
create function count_merchant(req int)
returns int
begin
declare cnum int ;
select count(*) into cnum from bgs_merchant where status = req;
return cnum;
end$
delimiter ;调用:
select count_merchant(3);
















