mysql存储过程中,IN用来输入参数,OUT用来输出参数,INOUT输入和输出。
IN参数
创建存储过程
mysql> delimiter //
mysql> create procedure proce1(IN count int)
-> begin
-> select count;
-> set count = 2;
-> select count;
-> end
-> //
1
2
3
4
5
6
7
8
9
执行存储过程
mysql> set @count = 1//
Query OK, 0 rows affected (0.00 sec)
mysql> call proce1(@count)//
+-------+
| count |
+-------+
| 1 |
+-------+
1 row in set (0.00 sec)
+-------+
| count |
+-------+
| 2 |
+-------+
1 row in set (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
mysql> select @count
-> //
+--------+
| @count |
+--------+
| 1 |
+--------+
1 row in set (0.00 sec)
mysql>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
在上面存储过程中,我们定义了一个参数@count,赋值为1,传入到存储过程中,在存储过程中赋值为2,当存储过程结束后,打印@count的值依旧是1.
OUT参数
创建存储过程
mysql> create procedure proce_out(OUT count int)
-> begin
-> select count;
-> set count = 2;
-> select count;
-> end//
Query OK, 0 rows affected (0.05 sec)
1
2
3
4
5
6
7
执行存储过程
mysql> set @count=1;
-> call proce_out(@count);
-> select @count;
-> //
Query OK, 0 rows affected (0.00 sec)
+-------+
| count |
+-------+
| NULL |
+-------+
1 row in set (0.00 sec)
+-------+
| count |
+-------+
| 2 |
+-------+
1 row in set (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
+--------+
| @count |
+--------+
| 2 |
+--------+
1 row in set (0.00 sec)
mysql>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
@count在外面赋值为1,因为参数是OUT类型,所以存储过程中打印出来为null,存储过程中赋值为2,存储过程结束,查看@count的值,依旧为2。
INOUT 输入输出参数
创建存储过程
mysql> create procedure proce_inout(INOUT count int)
-> begin
-> select count;
-> set count = 2;
-> select count;
-> end;
-> //
1
2
3
4
5
6
7
8
执行存储过程
mysql> set @count = 1;
-> call proce_inout(@count);
-> select @count;
-> //
Query OK, 0 rows affected (0.00 sec)
+-------+
| count |
+-------+
| 1 |
+-------+
1 row in set (0.00 sec)
+-------+
| count |
+-------+
| 2 |
+-------+
1 row in set (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
+--------+
| @count |
+--------+
| 2 |
+--------+
1 row in set (0.00 sec)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
我们在外面把@count赋值为1,存储过程里面获取到了值,然后赋值为2,存储过程结束后,打印出来的值依旧是2
————————————————
版权声明:本文为CSDN博主「徐海兴」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/u012326462/article/details/83445404