此处先来个demo:
(计算两个数之和)
create procedure pr_add
(
a int,
b int
)
begin
declare c int;
if a is null then
set a = 0;
end if;
f b is null then
set b = 0;
end if;
set c = a + b;
select c as sum;
end;
调用过程:
call pr_add(11,NULL);
创建 MySQL 存储过程的简单语法为:
create procedure 存储过程名字(【参数 参数类型】,【参数 参数类型】,……)
begin
MySQL 语句;
end;
调用mySql存储过程:
call 存储过程名字(【参数 参数类型】,【参数 参数类型】,……);
1、如果 MySQL 存储过程中包含多条 MySQL 语句,则需要 begin end 关键字。
create procedure pr_add
(
a int,
b int
)
begin
mysql statement 1 ...;
mysql statement 2 ...;
end;
2、MySQL 存储过程中的每条语句的末尾,都要加上分号 “;”
...
declare c int;
if a is null then
set a = 0;
end if;
...
end;
3、不能在 MySQL 存储过程中使用 “return” 关键字。
4、mySql存储过程参数没有默认值。