我有一个带有While的存储过程,在这个While中,我需要从动态SQL语句创建一个视图。 对于第一次迭代,我的视图包含良好的结果,但followinf迭代始终具有主要结果,而不是与新的预准备语句相对应的行...
如果我将WHILE替换为使用@a的不同值的创建视图的2倍,结果将达到预期。 关于While的准备好的语句似乎是一个问题...
DROP PROCEDURE IF EXISTS bug;
DELIMITER |
CREATE PROCEDURE bug()
BEGIN
DECLARE a INT(1);
DECLARE s VARCHAR(100) DEFAULT '';
SET @a = 1;
WHILE @a <= 2 DO
SET @s = concat('CREATE OR REPLACE VIEW bugview as SELECT ',@a,';');
select concat('@s=',@s);
PREPARE stmt FROM @s;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
select @a;
select * from bugview;
set @a = @a + 1;
END WHILE;
END|
DELIMITER ;
错误的结果(在最后一张表上,期望值应为2而不是1):
+------------------------------------------------+
| concat('@s=',@s) |
+------------------------------------------------+
| @s=CREATE OR REPLACE VIEW bugview as SELECT 1; |
+------------------------------------------------+
1 row in set (0.00 sec)
+------+
| @a |
+------+
| 1 |
+------+
1 row in set (0.01 sec)
+---+
| 1 |
+---+
| 1 |
+---+
1 row in set (0.01 sec)
+------------------------------------------------+
| concat('@s=',@s) |
+------------------------------------------------+
| @s=CREATE OR REPLACE VIEW bugview as SELECT 2; |
+------------------------------------------------+
1 row in set (0.01 sec)
+------+
| @a |
+------+
| 2 |
+------+
1 row in set (0.02 sec)
+---+
| 1 |
+---+
| 1 |
+---+
1 row in set (0.02 sec)