存储过程3:加法

来源:互联网 发布:手机特效软件安卓版 编辑:程序博客网 时间:2024/06/08 19:42
drop procedure if exists pr_add;  create procedure pr_add(in a int, in 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; //    call pr_add(10, 20);//set @a = 10;   set @b = 20;   call pr_add(@a, @b); //drop procedure if exists pr_add;  create procedure pr_add(in a int, in b int, out sum int)   begin    if a is null then   set a = 0;   end if;   if b is null then    set b = 0;   end if;   set sum = a + b;  end; //    call pr_add(10, 20, @sum);//select @sum;//


MySQL 存储过程参数如果不显式指定“in”、“out”、“inout”,则默认为“in”。习惯上,
对于是“in” 的参数,我们都不会显式指定。


MySQL 存储过程名字后面的“()”是必须的,即使没有一个参数,也需要“()”


MySQL 存储过程参数,不能在参数名称前加“@”,如:“@a int”。下面的创建存储过程
语法在 MySQL 中是错误的(在 SQL Server 中是正确的)。 MySQL 存储过程中的变量,不需要在变量名字前加“@”,虽然 MySQL 客户端用户变量要加个“@”。
create procedure pr_add
(   @a int, -- 错误   
    b int -- 正确   
)  

因为 MySQL 存储过程参数没有默认值,所以在调用 MySQL 存储过程时候,不能省略参数。可以用 null 来替代。

比如

call pr_add(null, 20, @sum);//select @sum;//

结果是20


原创粉丝点击