while salida procedimientos procedimiento parámetros investigar hacer for cómo condicionales con ciclos ciclo anidados almacenados almacenado mysql stored-procedures while-loop

salida - mysql ciclo



Mysql anidado al bucle en un procedimiento almacenado (1)

Esto se debe a que después de la primera iteración del bucle while externo, el valor de second_while_start ya es second_while_count lo que el bucle interno no se ejecuta, ya que second_while_start ya no es menor que second_while_count .

Para obtener su "resultado esperado", debe restablecer second_while_start.

DELIMITER $$ DROP PROCEDURE IF EXISTS `First_Sp` $$ CREATE DEFINER=`root`@`localhost` PROCEDURE `First_Sp`() BEGIN DECLARE first_while_start INTEGER DEFAULT 1; DECLARE second_while_start INTEGER DEFAULT 1; DECLARE first_while_count INTEGER DEFAULT 3; DECLARE second_while_count INTEGER DEFAULT 3; WHILE first_while_start < first_while_count DO WHILE second_while_start < second_while_count DO SELECT concat(first_while_start,'' - '',second_while_start) as result; SET second_while_start = second_while_start + 1; END WHILE; SET first_while_start = first_while_start + 1; /*here comes the important line:*/ SET second_while_start = 1; END WHILE; END $$ DELIMITER ;

actualmente estoy trabajando en un procedimiento almacenado en el que estoy usando un ciclo while en otro ciclo while. pero no estoy obteniendo el resultado esperado. el bucle más externo es la iteración solo una vez.

Estoy intentando seguir el código.

DELIMITER $$ DROP PROCEDURE IF EXISTS `First_Sp` $$ CREATE DEFINER=`root`@`localhost` PROCEDURE `First_Sp`() BEGIN DECLARE first_while_start INTEGER DEFAULT 1; DECLARE second_while_start INTEGER DEFAULT 1; DECLARE first_while_count INTEGER DEFAULT 3; DECLARE second_while_count INTEGER DEFAULT 3; WHILE first_while_start < first_while_count DO WHILE second_while_start < second_while_count DO SELECT concat(first_while_start,'' - '',second_while_start) as result; SET second_while_start = second_while_start + 1; END WHILE; SET first_while_start = first_while_start + 1; END WHILE; END $$ DELIMITER ;

Resultado:

mysql> call first_sp(); +--------+ | result | +--------+ | 1 - 1 | +--------+ 1 row in set (0.00 sec) +--------+ | result | +--------+ | 1 - 2 | +--------+ 1 row in set (0.00 sec) Query OK, 0 rows affected (0.00 sec).

También probé Repeat Loop. pero aún no hay SUERTE.

DELIMITER $$ DROP PROCEDURE IF EXISTS `First_Sp` $$ CREATE DEFINER=`root`@`localhost` PROCEDURE `First_Sp`() BEGIN DECLARE first_while_start INTEGER DEFAULT 1; DECLARE second_while_start INTEGER DEFAULT 1; DECLARE first_while_count INTEGER DEFAULT 3; DECLARE second_while_count INTEGER DEFAULT 3; REPEAT WHILE second_while_start < second_while_count DO SELECT concat(first_while_start,'' - '',second_while_start) as result; SET second_while_start = second_while_start + 1; END WHILE; SET first_while_start = first_while_start + 1; UNTIL first_while_start < first_while_count END REPEAT; END $$ DELIMITER ;

No soy mucho de SQL Developer. Estoy intentando.