ora-22160 element at index 1 does not exist oracle
Case :- When we are using PL/SQL collection with FORALL statement it's process collection using an index number. When one of index number are missing you can get this error.
Solve :- In addition from oracle 10g Oracle added two new clauses to the FORALL statement: INDICES OF and VALUES OF. They allow you to avoid the restriction on using densely filled collections. Instead of using an IN range of values, you can point to a collection (usually, but not necessarily, the same collection that is referenced inside the FORALL's DML statement) and say, in effect, "Use only those index values defined in that other collection" (INDICES OF) or "Use only index values that are found in the elements of that other collection" (VALUES OF).
CREATE OR REPLACE PROCEDURE DER_element_at_index_1_ERROR
IS
TYPE REC_DEP IS RECORD
(
DEPARTMENT_ID NUMBER(4),
DEPARTMENT_NAME VARCHAR2(30),
MANAGER_ID NUMBER(6),
LOCATION_ID NUMBER(4)
);
TYPE TABLE_DEP IS TABLE OF REC_DEP INDEX BY PLS_INTEGER;
T_DEP TABLE_DEP;
TYPE TT_DEPARTMENT_ID IS TABLE OF NUMBER(4) INDEX BY PLS_INTEGER;
TYPE TT_DEPARTMENT_NAME IS TABLE OF VARCHAR2(300) INDEX BY PLS_INTEGER;
TYPE TT_MANAGER_ID IS TABLE OF NUMBER(18,3) INDEX BY PLS_INTEGER;
TYPE TT_LOCATION_ID IS TABLE OF NUMBER(18,3) INDEX BY PLS_INTEGER;
T_DEPARTMENT_ID TT_DEPARTMENT_ID;
T_DEPARTMENT_NAME TT_DEPARTMENT_NAME;
T_MANAGER_ID TT_MANAGER_ID;
T_LOCATION_ID TT_LOCATION_ID;
V_INDEX NUMBER(10):=0;
V_SQL VARCHAR2(3000);
TYPE RC IS REF CURSOR;
C1 RC;
BEGIN
V_SQL:='SELECT DEPARTMENT_ID, DEPARTMENT_NAME, MANAGER_ID, LOCATION_ID FROM DEPARTMENTS';
OPEN C1 FOR V_SQL;
LOOP
FETCH C1 BULK COLLECT INTO T_DEP LIMIT 200;
FOR I IN T_DEP.FIRST .. T_DEP.LAST LOOP
V_INDEX:=V_INDEX+1;
T_DEPARTMENT_ID(V_INDEX):=T_DEP(I).DEPARTMENT_ID;
T_DEPARTMENT_NAME(V_INDEX):=T_DEP(I).DEPARTMENT_NAME;
T_MANAGER_ID(V_INDEX):=T_DEP(I).MANAGER_ID;
T_LOCATION_ID(V_INDEX):=T_DEP(I).LOCATION_ID;
END LOOP;
EXIT WHEN C1%NOTFOUND;
END LOOP;
FORALL INDX IN T_DEPARTMENT_ID.FIRST .. T_DEPARTMENT_ID.LAST
INSERT INTO D(DEPARTMENT_ID, DEPARTMENT_NAME, MANAGER_ID, LOCATION_ID)
VALUES(T_DEPARTMENT_ID(INDX),T_DEPARTMENT_NAME(INDX),T_MANAGER_ID(INDX),T_LOCATION_ID(INDX));
COMMIT;
T_DEPARTMENT_ID.DELETE;
T_DEPARTMENT_NAME.DELETE;
T_MANAGER_ID.DELETE;
T_LOCATION_ID.DELETE;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE(SQLERRM);
END;
BEGIN
FOR I IN 1 .. 5 LOOP
DER_element_at_index_1_ERROR;
END LOOP;
END;
ORA-22160: element at index [1] does not exist10
CREATE OR REPLACE PROCEDURE DER_element_at_index_1_ERROR
IS
TYPE REC_DEP IS RECORD
(
DEPARTMENT_ID NUMBER(4),
DEPARTMENT_NAME VARCHAR2(30),
MANAGER_ID NUMBER(6),
LOCATION_ID NUMBER(4)
);
TYPE TABLE_DEP IS TABLE OF REC_DEP INDEX BY PLS_INTEGER;
T_DEP TABLE_DEP;
TYPE TT_DEPARTMENT_ID IS TABLE OF NUMBER(4) INDEX BY PLS_INTEGER;
TYPE TT_DEPARTMENT_NAME IS TABLE OF VARCHAR2(300) INDEX BY PLS_INTEGER;
TYPE TT_MANAGER_ID IS TABLE OF NUMBER(18,3) INDEX BY PLS_INTEGER;
TYPE TT_LOCATION_ID IS TABLE OF NUMBER(18,3) INDEX BY PLS_INTEGER;
T_DEPARTMENT_ID TT_DEPARTMENT_ID;
T_DEPARTMENT_NAME TT_DEPARTMENT_NAME;
T_MANAGER_ID TT_MANAGER_ID;
T_LOCATION_ID TT_LOCATION_ID;
V_INDEX NUMBER(10):=0;
V_SQL VARCHAR2(3000);
TYPE RC IS REF CURSOR;
C1 RC;
BEGIN
V_SQL:='SELECT DEPARTMENT_ID, DEPARTMENT_NAME, MANAGER_ID, LOCATION_ID FROM DEPARTMENTS';
OPEN C1 FOR V_SQL;
LOOP
FETCH C1 BULK COLLECT INTO T_DEP LIMIT 200;
FOR I IN T_DEP.FIRST .. T_DEP.LAST LOOP
V_INDEX:=V_INDEX+1;
T_DEPARTMENT_ID(V_INDEX):=T_DEP(I).DEPARTMENT_ID;
T_DEPARTMENT_NAME(V_INDEX):=T_DEP(I).DEPARTMENT_NAME;
T_MANAGER_ID(V_INDEX):=T_DEP(I).MANAGER_ID;
T_LOCATION_ID(V_INDEX):=T_DEP(I).LOCATION_ID;
END LOOP;
EXIT WHEN C1%NOTFOUND;
END LOOP;
FORALL INDX IN T_DEPARTMENT_ID.FIRST .. T_DEPARTMENT_ID.LAST
INSERT INTO D(DEPARTMENT_ID, DEPARTMENT_NAME, MANAGER_ID, LOCATION_ID)
VALUES(T_DEPARTMENT_ID(INDX),T_DEPARTMENT_NAME(INDX),T_MANAGER_ID(INDX),T_LOCATION_ID(INDX));
COMMIT;
T_DEPARTMENT_ID.DELETE;
T_DEPARTMENT_NAME.DELETE;
T_MANAGER_ID.DELETE;
T_LOCATION_ID.DELETE;
V_INDEX:=0;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE(SQLERRM);
END;
SQL> BEGIN
2 FOR I IN 1 .. 5 LOOP
3
4 DER_element_at_index_1_ERROR;
5
6 END LOOP;
7
8 END;
9 /
PL/SQL procedure successfully completed.
Case :- When we are using PL/SQL collection with FORALL statement it's process collection using an index number. When one of index number are missing you can get this error.
Solve :- In addition from oracle 10g Oracle added two new clauses to the FORALL statement: INDICES OF and VALUES OF. They allow you to avoid the restriction on using densely filled collections. Instead of using an IN range of values, you can point to a collection (usually, but not necessarily, the same collection that is referenced inside the FORALL's DML statement) and say, in effect, "Use only those index values defined in that other collection" (INDICES OF) or "Use only index values that are found in the elements of that other collection" (VALUES OF).
CREATE OR REPLACE PROCEDURE DER_element_at_index_1_ERROR
IS
TYPE REC_DEP IS RECORD
(
DEPARTMENT_ID NUMBER(4),
DEPARTMENT_NAME VARCHAR2(30),
MANAGER_ID NUMBER(6),
LOCATION_ID NUMBER(4)
);
TYPE TABLE_DEP IS TABLE OF REC_DEP INDEX BY PLS_INTEGER;
T_DEP TABLE_DEP;
TYPE TT_DEPARTMENT_ID IS TABLE OF NUMBER(4) INDEX BY PLS_INTEGER;
TYPE TT_DEPARTMENT_NAME IS TABLE OF VARCHAR2(300) INDEX BY PLS_INTEGER;
TYPE TT_MANAGER_ID IS TABLE OF NUMBER(18,3) INDEX BY PLS_INTEGER;
TYPE TT_LOCATION_ID IS TABLE OF NUMBER(18,3) INDEX BY PLS_INTEGER;
T_DEPARTMENT_ID TT_DEPARTMENT_ID;
T_DEPARTMENT_NAME TT_DEPARTMENT_NAME;
T_MANAGER_ID TT_MANAGER_ID;
T_LOCATION_ID TT_LOCATION_ID;
V_INDEX NUMBER(10):=0;
V_SQL VARCHAR2(3000);
TYPE RC IS REF CURSOR;
C1 RC;
BEGIN
V_SQL:='SELECT DEPARTMENT_ID, DEPARTMENT_NAME, MANAGER_ID, LOCATION_ID FROM DEPARTMENTS';
OPEN C1 FOR V_SQL;
LOOP
FETCH C1 BULK COLLECT INTO T_DEP LIMIT 200;
FOR I IN T_DEP.FIRST .. T_DEP.LAST LOOP
V_INDEX:=V_INDEX+1;
T_DEPARTMENT_ID(V_INDEX):=T_DEP(I).DEPARTMENT_ID;
T_DEPARTMENT_NAME(V_INDEX):=T_DEP(I).DEPARTMENT_NAME;
T_MANAGER_ID(V_INDEX):=T_DEP(I).MANAGER_ID;
T_LOCATION_ID(V_INDEX):=T_DEP(I).LOCATION_ID;
END LOOP;
EXIT WHEN C1%NOTFOUND;
END LOOP;
FORALL INDX IN T_DEPARTMENT_ID.FIRST .. T_DEPARTMENT_ID.LAST
INSERT INTO D(DEPARTMENT_ID, DEPARTMENT_NAME, MANAGER_ID, LOCATION_ID)
VALUES(T_DEPARTMENT_ID(INDX),T_DEPARTMENT_NAME(INDX),T_MANAGER_ID(INDX),T_LOCATION_ID(INDX));
COMMIT;
T_DEPARTMENT_ID.DELETE;
T_DEPARTMENT_NAME.DELETE;
T_MANAGER_ID.DELETE;
T_LOCATION_ID.DELETE;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE(SQLERRM);
END;
BEGIN
FOR I IN 1 .. 5 LOOP
DER_element_at_index_1_ERROR;
END LOOP;
END;
ORA-22160: element at index [1] does not exist10
CREATE OR REPLACE PROCEDURE DER_element_at_index_1_ERROR
IS
TYPE REC_DEP IS RECORD
(
DEPARTMENT_ID NUMBER(4),
DEPARTMENT_NAME VARCHAR2(30),
MANAGER_ID NUMBER(6),
LOCATION_ID NUMBER(4)
);
TYPE TABLE_DEP IS TABLE OF REC_DEP INDEX BY PLS_INTEGER;
T_DEP TABLE_DEP;
TYPE TT_DEPARTMENT_ID IS TABLE OF NUMBER(4) INDEX BY PLS_INTEGER;
TYPE TT_DEPARTMENT_NAME IS TABLE OF VARCHAR2(300) INDEX BY PLS_INTEGER;
TYPE TT_MANAGER_ID IS TABLE OF NUMBER(18,3) INDEX BY PLS_INTEGER;
TYPE TT_LOCATION_ID IS TABLE OF NUMBER(18,3) INDEX BY PLS_INTEGER;
T_DEPARTMENT_ID TT_DEPARTMENT_ID;
T_DEPARTMENT_NAME TT_DEPARTMENT_NAME;
T_MANAGER_ID TT_MANAGER_ID;
T_LOCATION_ID TT_LOCATION_ID;
V_INDEX NUMBER(10):=0;
V_SQL VARCHAR2(3000);
TYPE RC IS REF CURSOR;
C1 RC;
BEGIN
V_SQL:='SELECT DEPARTMENT_ID, DEPARTMENT_NAME, MANAGER_ID, LOCATION_ID FROM DEPARTMENTS';
OPEN C1 FOR V_SQL;
LOOP
FETCH C1 BULK COLLECT INTO T_DEP LIMIT 200;
FOR I IN T_DEP.FIRST .. T_DEP.LAST LOOP
V_INDEX:=V_INDEX+1;
T_DEPARTMENT_ID(V_INDEX):=T_DEP(I).DEPARTMENT_ID;
T_DEPARTMENT_NAME(V_INDEX):=T_DEP(I).DEPARTMENT_NAME;
T_MANAGER_ID(V_INDEX):=T_DEP(I).MANAGER_ID;
T_LOCATION_ID(V_INDEX):=T_DEP(I).LOCATION_ID;
END LOOP;
EXIT WHEN C1%NOTFOUND;
END LOOP;
FORALL INDX IN T_DEPARTMENT_ID.FIRST .. T_DEPARTMENT_ID.LAST
INSERT INTO D(DEPARTMENT_ID, DEPARTMENT_NAME, MANAGER_ID, LOCATION_ID)
VALUES(T_DEPARTMENT_ID(INDX),T_DEPARTMENT_NAME(INDX),T_MANAGER_ID(INDX),T_LOCATION_ID(INDX));
COMMIT;
T_DEPARTMENT_ID.DELETE;
T_DEPARTMENT_NAME.DELETE;
T_MANAGER_ID.DELETE;
T_LOCATION_ID.DELETE;
V_INDEX:=0;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE(SQLERRM);
END;
SQL> BEGIN
2 FOR I IN 1 .. 5 LOOP
3
4 DER_element_at_index_1_ERROR;
5
6 END LOOP;
7
8 END;
9 /
PL/SQL procedure successfully completed.