Ver Mensaje Individual
  #3 (permalink)  
Antiguo 02/07/2009, 00:50
rocolobo7
 
Fecha de Ingreso: julio-2009
Mensajes: 6
Antigüedad: 14 años, 9 meses
Puntos: 0
Respuesta: Modificar auto_increment desde Procedimiento Almac

Sí, claro. Primero de todo darte las gracias por tu tiempo e interés.

Te adjunto el código completo:

DELIMITER $$

CREATE PROCEDURE ReOrdenar()
BEGIN


DECLARE done INTEGER DEFAULT 0;
DECLARE a CHAR(10);
DECLARE b INTEGER;
DECLARE c CHAR(255);
DECLARE cur1 CURSOR FOR SELECT id, Identificador FROM tabla_auxiliar;
DECLARE cur2 CURSOR FOR SELECT id FROM tabla_auxiliar;
DECLARE cur3 CURSOR FOR SELECT Max(id) FROM peticiones;


DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET done = 1;

OPEN cur1;
OPEN cur2;
OPEN cur3;


FETCH cur1 INTO b,a;

REPEAT

UPDATE peticiones SET peticiones.identificador = Concat(Left(peticiones.identificador,4),RTRIM(SUBS TRING(peticiones.identificador,5))-1)
WHERE peticiones.id > b and Left(peticiones.identificador,3)=Left(a,3);

DELETE FROM historico_descripcion WHERE historico_descripcion.id_peticion=b;

FETCH cur1 INTO b,a;

UNTIL done END REPEAT;


FETCH cur2 INTO b;

REPEAT

UPDATE peticiones SET peticiones.id = peticiones.id - 1
WHERE peticiones.id >= b;

UPDATE historico_descripcion SET historico_descripcion.id_peticion = historico_descripcion.id_peticion - 1
WHERE historico_descripcion.id_peticion >= b;

FETCH cur2 INTO b;

UNTIL done END REPEAT;


FETCH cur3 INTO b;

ALTER TABLE peticiones AUTO_INCREMENT=b+1;

DELETE FROM tabla_auxiliar;


CLOSE cur1;
CLOSE cur2;
CLOSE cur3;

END $$

DELIMITER;