1

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_ 

1 Answer 1

2

I suspect that are just missing an extra BEGIN in your updated code. An EXCEPTION clause always matches to a BEGIN and an END. In the code that you posted, the EXCEPTION matches the procedure's BEGIN. You need it to match the BEGIN of the nested PL/SQL block.

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
  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;

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Ok. That explains a lot. I actually misunderstood. I gotta correct it. Anyway, let's say for some reason I need to do that. I update my question with what you suggested but there are some errors.
Thank you! That's what I was looking for. And also follow your advise on the other thread.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.