MySQL创建存储过程

来源:互联网 发布:哪下载软件安全 编辑:程序博客网 时间:2024/05/16 14:41

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 ; //
0 0