En este post os pondremos una select para recuperar la tabla y la columna donde se produce este tipo de error:
ORA-02291: integrity constraint (MANTE90.PRUEBA2_FK) violated – parent key not
found
Primero crearemos una tabla e insertaremos un registro. Posteriormente le añadiremos la clave primaria.
CREATE TABLE PRUEBA ( ID NUMBER NOT NULL, DESCRIPCION VARCHAR2(10)); INSERT INTO PRUEBA(1,'HOLA'); ALTER TABLE PRUEBA ADD CONSTRAINT PRUEBA_PK PRIMARY KEY(ID); |
Ahora creamos la segunda tabla, que tendra una foreign key a la tabla anterior.
CREATE TABLE PRUEBA2(COL1 NUMBER); ALTER TABLE PRUEBA2 ADD (CONSTRAINT PRUEBA2_FK FOREIGN KEY (COL1) REFERENCES PRUEBA(ID)); |
Ahora insertaremos en esta tabla (PRUEBA2) un valor que no este y Oracle nos devolverá un error tipo:
ORA-02291: integrity constraint (MANTE90.PRUEBA2_FK) violated – parent key not
found
Ahora recuperaremos del error (SQLERRM) el número del error que devuelve y lo buscaremos en la tabla de constrains en las columnas.
SET SERVEROUTPUT ON DECLARE COLUM_ERR USER_CONS_COLUMNS.COLUMN_NAME%TYPE; L_CONS USER_CONSTRAINTS.CONSTRAINT_NAME%TYPE; L_START NUMBER; L_STOP NUMBER; E_CONSVIOLATION EXCEPTION; PRAGMA EXCEPTION_INIT(E_CONSVIOLATION, -02291); BEGIN INSERT INTO PRUEBA2 VALUES(3); EXCEPTION WHEN E_CONSVIOLATION THEN L_START := INSTR( SQLERRM, '(' ) + 1; L_STOP := INSTR( SQLERRM, ')' ); L_CONS := SUBSTR(SQLERRM, L_START, L_STOP-L_START); L_START := INSTR( L_CONS, '.' ) + 1; L_CONS := SUBSTR(L_CONS, L_START, L_STOP-L_START); SELECT COLUMN_NAME INTO COLUM_ERR FROM USER_CONS_COLUMNS WHERE TABLE_NAME = 'PRUEBA2' AND CONSTRAINT_NAME = L_CONS; DBMS_OUTPUT.PUT_LINE('TABLA PRUEBA2. COLUMNA: ' || COLUM_ERR); WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE(SQLERRM); END; / TABLA PRUEBA2. COLUMNA: COL1 PL/SQL PROCEDURE successfully completed. |