当前位置:Gxlcms > 数据库问题 > Mysql储存过程6: in / out / inout

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

时间:2021-07-01 10:21:17 帮助过:23人阅读

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类似, 也就是要变量定义型式: @变量名。

 

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

标签:roc   --   pre   过程   bsp   变量   select   abc   调用   

人气教程排行