页次: 1
存储过程示例可以下
msyql 存储过程中有三种参数 in, out, inout
in -- 表示输入的参数 , 这个值是不能返回的的
out -- 如果在过程中,修改了这个值,这个值是可以返回的
inout -- 调用的时候指定,可以返回。
三个例子
第一个 in
mysql> delimiter // mysql> create procedure f_in(in x int) -> begin -> select x*x; -> end; -> // Query OK, 0 rows affected (0.01 sec) mysql> delimiter ; mysql> call f_in(1); +------+ | x*x | +------+ | 1 | +------+ 1 row in set (0.00 sec) Query OK, 0 rows affected (0.00 sec) mysql> call f_in(2); +------+ | x*x | +------+ | 4 | +------+ 1 row in set (0.00 sec)
第二个例子 out
创建过程
delimiter // create procedure f_out (out x int) begin set x=100; end; // delimiter ;
调用过程
mysql> set @z = 1; Query OK, 0 rows affected (0.00 sec) mysql> call f_out(@z); Query OK, 0 rows affected (0.00 sec) mysql> select @z; +------+ | @z | +------+ | 100 | +------+ 1 row in set (0.00 sec)
这里可以看到 @z 的值已经被修改了。
第三例子:
delimiter // create procedure f_inout(inout x int) begin select x; set x = 100; end; // delimiter ;
调用的结果
mysql> call f_inout(@a); +------+ | x | +------+ | NULL | +------+ 1 row in set (0.00 sec) Query OK, 0 rows affected (0.00 sec) mysql> select @a; +------+ | @a | +------+ | 100 | +------+ 1 row in set (0.00 sec)
out 和 inout 的区别 可以看这个
delimiter // create procedure f_out2 (out x int) begin select x; set x=100; end; // delimiter ;
这里参数x是out ,在过程中进行了 select 操作, 是获取不到值的。
mysql> set @a = 1; Query OK, 0 rows affected (0.00 sec) mysql> call f_out2(@a); +------+ | x | +------+ | NULL | +------+ 1 row in set (0.00 sec) Query OK, 0 rows affected (0.01 sec)
ipbbs.net
离线
页次: 1