Mysql储存过程6: in / out / inout

in 为向函数传送进去的值

out 为函数向外返回的值

intout 传送进去的值, 并且还返回这个值

 

    create procedure q1(in number int,out name varchar(100))
      begin
        if number > 1 then
        select 'true';
        else
        select 'false';
        end if;
      end$

调用时:

call q1(1, @value);

注意, 第二个参数要为变量定义的型式。

这个函数并没有向外发送改变后的name值, 所以调用后 select @value 为null。

 

再看看out:

mysql>     create procedure qq(number int,inout name varchar(100))
    ->       begin
    ->         if number > 1 then
    ->         select 'true';
    ->         else
    ->         select 'false';
    ->         end if;
    ->         set name=user();
    ->       end$
Query OK, 0 rows affected (0.00 sec)

mysql> call qq(1,@as)$
+-------+
| false |
+-------+
| false |
+-------+
1 row in set (0.00 sec)

Query OK, 0 rows affected (0.01 sec)

mysql> select @as$
+----------------+
| @as            |
+----------------+
| root@localhost |
+----------------+
1 row in set (0.00 sec)

mysql>

 

inout例子:

mysql> create procedure qqq(inout name varchar(100))
    ->       begin
    ->         set name=database();
    ->       end$
Query OK, 0 rows affected (0.00 sec)

mysql> call qqq(1)$
ERROR 1414 (42000): OUT or INOUT argument 1 for routine test.qqq is not a variable or NEW pseudo-var
iable in BEFORE trigger
mysql> call qqq(@abc)$
Query OK, 0 rows affected (0.00 sec)

mysql> select @abc$
+------+
| @abc |
+------+
| test |
+------+
1 row in set (0.00 sec)

mysql>

注意参数型式, 因为他要发送回来, 这个inout的参数型式要跟out类似, 也就是要变量定义型式: @变量名。

 

posted on 2017-07-04 07:43  Perl6  阅读(213)  评论(0编辑  收藏  举报

导航