Mysql储存过程2:变量定义与参数传递

时间:2022-03-26 19:41:13
#储存过程 中的变量定义
declare 变量名 类型 可选类型 -- 跟建表差不多 create procedure p()
begin
declare age int default(18);
declare number int default 1;
select age+number;
end$
/*
mysql> create procedure p()
-> begin
-> declare age int default(18);
-> declare number int default 1;
-> select age+number;
-> end$
Query OK, 0 rows affected (0.00 sec) mysql> call p$
+------------+
| age+number |
+------------+
| 19 |
+------------+
1 row in set (0.00 sec) Query OK, 0 rows affected (0.01 sec)
*/
#储存过程 中, 变量可以运算
#运算结果赋修值给变量用: set age := 运算式
#set age := age + 2
#参数传递

mysql> create procedure p2(number int, age int)
-> begin
-> select age+number;
-> end$
Query OK, 0 rows affected (0.00 sec) mysql> call p2(1,2)$
+------------+
| age+number |
+------------+
| 3 |
+------------+
1 row in set (0.00 sec) Query OK, 0 rows affected (0.01 sec) mysql>