Fork me on GitHub

存储过程的参数

存储过程传参:存储过程的括号里,可以声明参数。 语法是:create procedure p([in/out/inout] 参数名 参数类型 ..),MySQL 存储过程参数如果不显式指定in、out、inout,则默认为in

in:传入function的参数

create procedure p7(in n int)
begin
    declare num int default 0;
    declare total int default 0;

    while num <=n do
        set total := total + num;
        set num := num + 1;
    end while;

    select total;
end$

调用:call(100)-------->5050(1+2+3+....+100)/call(10)--------------->55(1+2+3+.........+10)

out:可以理解为某个function要改变的外部变量,不管这个变量在外部是什么值,在内部的初始值都是null,而内部对它的影响将改变这个外部变量值,即函数计算得到的值映射到函数外部

create procedure p8(in n int,out total int)
begin
    declare num int default 0;
    set total := 0;
    while num <=n do
        set total := total + num;
        set num := num + 1;
    end while;
end$

调用:call p8(10,@c);   select @c;----------------->打印55

inout:为一个function在内部声明了global ,并可能对该变量值进行修改

create procedure p9(inout age int)
begin
    set age := age + 20;
end$

调用:set @age := 20;    call p9(@age);    select @age;     打印40      

 

posted @ 2015-01-02 15:57  龙族小龙  阅读(980)  评论(0编辑  收藏  举报