mysql> DELIMITER $$
mysql> drop PROCEDURE sp_test_switch$$
Query OK,
0
rows affected (
0.01
sec)
mysql> CREATE PROCEDURE sp_test_switch()
-> BEGIN
-> declare a
int
;
-> declare b varchar(
5000
);
-> set a=
1
;
-> set b=
''
;
->
while
a<
10
do
-> set b = concat(b,
','
,a);
-> set a=a+
1
;
-> end
while
;
-> select b;
-> END
-> $$
Query OK,
0
rows affected (
0.00
sec)
mysql> DELIMITER ;
mysql>
mysql> call sp_test_switch();
+--------------------+
| b |
+--------------------+
| ,
1
,
2
,
3
,
4
,
5
,
6
,
7
,
8
,
9
|
+--------------------+
1
row in set (
0.00
sec)
Query OK,
0
rows affected (
0.00
sec)
mysql>
MySQL
存储过程的变量定义:declare和直接set @var什么区别?
我的理解是, declare定义的是局部变量, 只能用在存储过程或函数中, 其有效区间就是存储过程或函数中声明该变量的begin end区间.而@var属于用户变量(自己声明的), 其有效区间就是该session, 你既可以在存储过程或函数中使用, 也可以在自己写的SQL语句中使用.还有@不需要声明类型,declare必须指定类型