存储过程与函数
MySQL中的变量
局部变量
select column_1 into from table_1
-- into 关键字为变量赋值,只能存一个元素
declare name type [default 0];
-- 只能用在代码块中使用declare声明局部变量name
-- 指定初始值是0;
set name=10;
-- 修改局部变量为10
用户变量
客户端关闭用户变量清除
set @name = 0;
set @name := 0;
-- 不需要指定变量类型
select @name := 0;
-- 使用select语句定义用户变量必须使用:=
-- 若使用=的话,则会被当成等于关系运算符
MySQL中存储过程
create procedure procedure_1(
[in|out|inout] arguments type,
...
-- in是输入参数,省略不写则默认是in
-- out是输出参数
-- inout即时输入参数又是输出参数
)
begin -- 包裹代码块,相当于{
end; -- 包裹代码块,相当于}
call procedure_1(arguments);
-- 调用存储过程
-- 对于输出参数,无需定义,也可以使用
drop procedure procedure_1;
-- 删除存储过程
MySQL中的函数
create function function_1(
in|out|inout arguments type,
...
)returns type -- 定义返回值类型,return要加s
begin
return 0 -- 函数必须要有返回值
end;
select function_1(arguments);
-- 与mysql内置函数调用方法一致
drop function function_1;
-- 删除函数
Comments NOTHING