您的位置:首页 > 数据库 > MySQL

MySQL创建存储过程

2016-04-26 21:10 495 查看

MySQL创建存储过程

drop procedure test_add if exists;
-- 创建存储过程(不可以使用create or replace)
create  procedure test_add(
a INT,
b INT
)
BEGIN
declare c int;
if a is null then
set a = 0;
end if;
if b is null then
set b = 0;
end if;
set c = a + b;
select c as sum;
end;

--运行
set @a=10;
set @b=10;
call test_add(@a,@b);


以上是在navicat中可以,但是在workbench或mysql command client中不行。这是因为navicat会自动设置不同的分隔符,做了处理。在workbench和mysql command client中要使用以下的格式:

-- 设置Delimiter,防止和存储过程中的冲突
Delimiter //;
create procedure test_add(
a int,
b int
)
BEGIN
declare c int;
if a is null then
set a = 0;
end if;
if b is null then
set b = 0;
end if;
set c = a + b;
select c as sum;
end; //
-- 恢复默认的Delimiter;
Delimiter ; //
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: