WHILE Loop in MySql procedure returned multiple result -
i using next stored procedure
delimiter // create procedure sp_while (in param1 int) begin declare variable1 int; set variable1 = 0; while variable1 < param1 select variable1 id; set variable1 = variable1 + 1; end while; end // phone call sp_while (5); to next result:
+----+ | id | +----+ | 0 | | 1 | | 2 | | 3 | | 4 | +----+ but show 5 different result in 5 different window. how can solve this? please help me giving valuable suggestion.
yes thats how more happen. avoid can create temporary table , store values in there , select @ end as
delimiter // create procedure sp_while (in param1 int) begin declare variable1 int; set variable1 = 0; create temporary table test_1 (id int); while variable1 < param1 insert test_1 values (variable1); set variable1 = variable1 + 1; end while; select * test_1 ; end // delimiter ; so how looks in mysql
mysql> delimiter // mysql> create procedure sp_while (in param1 int) -> begin -> declare variable1 int; -> set variable1 = 0; -> create temporary table test_1 (id int); -> while variable1 < param1 -> insert test_1 values (variable1); -> set variable1 = variable1 + 1; -> end while; -> select * test_1 ; -> end // query ok, 0 rows affected (0.01 sec) mysql> delimiter ; mysql> phone call sp_while (5) ; +------+ | id | +------+ | 0 | | 1 | | 2 | | 3 | | 4 | +------+ 5 rows in set (0.03 sec) mysql stored-procedures
No comments:
Post a Comment