Oracle - EXCEPCION dentro de un bucle

 
Vista:

EXCEPCION dentro de un bucle

Publicado por vlg (7 intervenciones) el 30/06/2006 13:52:46
Hola mi intencion es crear una excepcion dentro de un bucle con el fin de que si la lectura de una fecha es incorrecta NO SALGA DEL BUCLE solo me muestre la fecha en concreto
la sintaxis que tengo es la siguiente:

create or replace procedure ActFechaRestestIPLOTES is

ptotaltablas varchar2(100);

-- declaracion cursor
CURSOR tablas IS
select CADUCI from iplotes lot where lot.RETEST IS NULL;
begin

-- abrir cursor
OPEN tablas;

-- recorrer cursor
LOOP
FETCH tablas INTO ptotaltablas;
EXIT WHEN tablas%NOTFOUND;
dbms_output.put_line('FECH->'||to_date(ptotaltablas,'yyyymmdd'));
EXCEPTION
when others then dbms_output.put_line('error->'||ptotaltablas);
END LOOP;
-- cerrar cursor
CLOSE tablas;
end;

Este codigo me da error ya que parece ser que la EXCEPCION no puede estar dentro del bucle, otra manera que he probado es la siguiente:

create or replace procedure ActFechaRestestIPLOTES is

ptotaltablas varchar2(100);

-- declaracion cursor
CURSOR tablas IS
select CADUCI from iplotes lot where lot.RETEST IS NULL;

begin
-- abrir cursor

OPEN tablas;
-- recorrer cursor

LOOP
FETCH tablas INTO ptotaltablas;
EXIT WHEN tablas%NOTFOUND;
dbms_output.put_line('FECH->'||to_date(ptotaltablas,'yyyymmdd'));

END LOOP;

EXCEPTION
when others then dbms_output.put_line('error->'||ptotaltablas);
-- cerrar cursor
CLOSE tablas;
end;

Pero aqui lo que pasa es que cuando se produce una excepcion rompe el bucle y me muestra el error, LO QUE INTENTO ES QUE EL BUCLE REALICE TODOS LOS REGISTROS DEL CURSOR INDEPENDIENTEMENTE DE SI HAY ERROR O NO (la excepcion deberia de estar dentro del bucle)

ESPERO QUE ME PODAIS AYUDAR MUCHAS GRACIAS
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder

RE:EXCEPCION dentro de un bucle

Publicado por Ricardo (84 intervenciones) el 19/07/2006 18:21:44
Probá con este código:

--------------------------------------------------------------------------------------------------------------------------
create or replace procedure ActFechaRestestIPLOTES is

ptotaltablas varchar2(100);

-- declaracion cursor
CURSOR tablas IS
select CADUCI from iplotes lot where lot.RETEST IS NULL;
begin

-- abrir cursor
OPEN tablas;

-- recorrer cursor
LOOP
FETCH tablas INTO ptotaltablas;
EXIT WHEN tablas%NOTFOUND;
BEGIN
dbms_output.put_line('FECH->'||to_date(ptotaltablas,'yyyymmdd'));
EXCEPTION
when others then dbms_output.put_line('error->'||ptotaltablas);
END;
END LOOP;
-- cerrar cursor
CLOSE tablas;
end;
--------------------------------------------------------------------------------------------------------------------------

Lo que agregué es el exception en un 'bloque PL-SQL'. Siempre que hagas exception,
vas a tener que usar la forma:

Begin
... comandos
Exception
<Tipo de excepción, ej "when others> then
... comandos
End;

Saludos & Suerte
Ricardo.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar