My stored procedure looks like follows:
sqlQuery := 'DROP INDEX idArchivoIndex';
EXECUTE IMMEDIATE sqlQuery;
EXCEPTION --En caso de que no exista el índice capturamos la excepcion
WHEN index_not_exists THEN NULL; --y la ignoramos
sqlQuery := 'CREATE INDEX idArchivoIndex'||
' ON '||qusuario||' (id_archivo)';
EXECUTE IMMEDIATE sqlQuery;
doresetvalidacion(qusuario, idarchivo);
IF (tipoDependencia = 'PEC') THEN
dovalidapec(qusuario,qaniofiscal,idarchivo,imprimirMensajes);
COMMIT;
ELSIF (tipoDependencia = 'SAGARPA') THEN
dovalidacionpec(qusuario,qaniofiscal,idarchivo,imprimirMensajes);
COMMIT;
END IF;
If the exception is not raised the procedure just drops the index but no index is recreated ! I thought that this part of the code
EXCEPTION
WHEN index_not_exists THEN NULL;
Handled the error and then continue with the code below it. Now that I see the results what’s after the EXCEPTION is executed if and only if the exception was raised.
What I want is to simplify my code, I don’t want to copy-paste the same block of code before the EXCEPTION clause just to make it work as I expect. Is there a way to achieve it? Maybe with a nested BEGIN ... END block? Or will I have to make a separate procedure to reuse code?
Cheers.
UPDATE
create or replace
PROCEDURE DOVALIDAINFORMACION
(
QARCHIVO IN VARCHAR2
, QUSUARIO IN VARCHAR2
, QANIOFISCAL IN VARCHAR2
) AS
imprimirMensajes CHAR;
tipoDependencia VARCHAR2(25);
idArchivo NUMBER;
sqlQuery VARCHAR2(100);
index_not_exists EXCEPTION;
PRAGMA EXCEPTION_INIT(index_not_exists, -1418);
BEGIN
sqlQuery := 'DROP INDEX idArchivoIndex';
EXECUTE IMMEDIATE sqlQuery;
----------------------
EXCEPTION --En caso de que no exista el índice capturamos la excepcion
WHEN index_not_exists THEN --y la ignoramos
NULL;
END;
----------------------
sqlQuery := 'CREATE INDEX idArchivoIndex'||
' ON '||qusuario||' (id_archivo)';
EXECUTE IMMEDIATE sqlQuery;
doresetvalidacion(qusuario, idarchivo);
IF (tipoDependencia = 'PEC') THEN
dovalidapec(qusuario,qaniofiscal,idarchivo,imprimirMensajes);
COMMIT;
ELSIF (tipoDependencia = 'SAGARPA') THEN
dovalidacionpec(qusuario,qaniofiscal,idarchivo,imprimirMensajes);
COMMIT;
END IF;
END DOVALIDAINFORMACION;
But can’t compile the procedure.
Error(32,3): PLS-00103: Se ha encontrado el símbolo "SQLQUERY"
Error(33,48): PLS-00103: Se ha encontrado el símbolo ";" cuando se esperaba uno de los siguientes: ) , * & = - + < / > at in is mod remainder not rem <an exponent (**)> <> or != or ~= >= <= <> and or like LIKE2_ LIKE4_ LIKEC_ between || member SUBMULTISET_
I suspect that are just missing an extra
BEGINin your updated code. AnEXCEPTIONclause always matches to aBEGINand anEND. In the code that you posted, theEXCEPTIONmatches the procedure’sBEGIN. You need it to match theBEGINof the nested PL/SQL block.As an aside, it seems odd to drop and then immediately re-create an index in a PL/SQL block. If this is somehow related to your question about recreating an index after a load, I’m afraid that you may have misunderstood my answer. In my earlier answer, I was pointing out that it may be more efficient to drop the index, load your 10 million rows of data, and then re-create the index. Assuming that the loads are happening in the stored procedure calls you are making in this code, you would want the index to be re-created after the loads are complete.