Skip to content

Instantly share code, notes, and snippets.

@aduartem
Last active May 21, 2022 19:43
Show Gist options
  • Save aduartem/0556a6b2c1091971c8216ebd3668ed24 to your computer and use it in GitHub Desktop.
Save aduartem/0556a6b2c1091971c8216ebd3668ed24 to your computer and use it in GitHub Desktop.
PL/SQL - Ejemplo de cursores en procedimientos almacenados Oracle (11g)
CREATE OR REPLACE PROCEDURE SP_PRUEBA (
  RESULTADO OUT NUMBER,  
  MSG OUT VARCHAR2
) AS 

vERRORMSG VARCHAR2(250);
pROL_ID NUMBER;

BEGIN
  
  SET TRANSACTION ISOLATION LEVEL READ COMMITTED;

  DECLARE
      CURSOR ROLES_MODULO
      IS
        SELECT ROL_ID FROM ROLES_MOD WHERE MOD_ID = 181;
  BEGIN
      OPEN ROLES_MODULO;
      LOOP
          FETCH ROLES_MODULO INTO pROL_ID;
          EXIT WHEN ROLES_MODULO%notfound;
          
          BEGIN
            INSERT INTO PRUEBA_1 (ROL_ID) VALUES (pROL_ID);
          END;
          
      END LOOP;
      CLOSE ROLES_MODULO;
  END;
  
  RESULTADO := 1;
  
  COMMIT;

EXCEPTION
    WHEN OTHERS THEN
        ROLLBACK;
        RESULTADO := -1;
        vERRORMSG := SUBSTR (SQLERRM, 1, 200);
        MSG := vERRORMSG;
        dbms_output.put_line (verrormsg);
END SP_PRUEBA;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment