Let's create a Type as an Oracle Object to be reference from both PL/SQL and SQL
CREATE TYPE SOME_ARRAY_REC AS OBJECT
(
COLUM1 VARCHAR2(100)
);
/
Now, DB object as nested table of the type we created above
CREATE TYPE SOME_ARRAY_TAB AS TABLE OF SOME_ARRAY_REC;
/
The PL/SQL
DECLARE
PL_ARRAY_REC TAB1%ROWTYPE;
TYPE PL_ARRAY_TAB IS TABLE OF PL_ARRAY_REC
INDEX BY PLS_INTEGER;
v_PL_ARRAY PL_ARRAY_TAB;
v_ARRAY SOME_ARRAY_TAB := SOME_ARRAY_TAB();
BEGIN
/* Bulk collect the records into local PL/SQL type */
SELECT COLUM1
BULK COLLECT INTO v_PL_ARRAY
FROM TAB1 WHERE COLUMN2=P_COL2;
/* To use it in SQL we have to use a SQL compatible Type*/
/* So copy the contents from local type to SQL compatible type */
FOR I IN 1..v_PL_ARRAY.COUNT
LOOP
v_ARRAY.EXTEND;
v_ARRAY(I) := SOME_ARRAY_REC(v_PL_ARRAY(I).COLUM1);
END LOOP;
/* Use TABLE() to cast the SQL compatible Nested table as normal table */
SELECT * BULK COLLECT INTO SOME_VARRAY
FROM TAB2
WHERE COLUMN1 IN (SELECT COLUM1 FROM TABLE(v_ARRAY));
END;
/
PS: I would rather prefer the SubQuery
TYPEis the Dynamic Array you look for.! or simply a sub-query?SELECT * FROM TAB2 WHERE TAB2.COLUMN1 IN (SELECT COLUM1 FROM TAB1 WHERE COLUMN2=P_COL2)? If so, I don't like that. Then could you please tell me how I would go theTYPEway?