Updated November, 23 2023

BULK COLLECT INTO#

To emulate the BULK COLLECT INTO clause, we create an aggregate function for each collection type.

This is an example of creating an aggregate for associative collection type:

SELECT BULK COLLECT INTO#

To emulate select bulk collect, we call aggregate for each value and insert them into the temporary record variable. After selecting we assign each field to the corresponding collection variable.

 1--Oracle
 2DECLARE
 3...
 4BEGIN
 5    SELECT empno, ename, job, hiredate, sal, comm, deptno
 6    BULK COLLECT
 7       INTO t_empno, t_ename, t_job, t_hiredate, t_sal, t_comm, t_deptno
 8       FROM emp;
 9    ...
10END;

SELECT BULK COLLECT INTO ROWTYPE#

Emulation of select bulk collect into %ROWTYPE collection is similar to regular select bulk collect, but we must specify an asterisk with the source name(alias).

 1--Oracle
 2DECLARE
 3    TYPE emp_tbl IS TABLE OF emp%ROWTYPE INDEX BY BINARY_INTEGER;
 4    t_emp        emp_tbl;
 5BEGIN
 6 SELECT *
 7    BULK COLLECT
 8       INTO t_emp
 9        FROM emp;
10  ...
11END;

FETCH BULK COLLECT INTO#

To aggregate values from the cursor, we create a fetch loop and store each fetched column in elem field of the corresponding collection variable. To aggregate values, we’re using lbr$accum function without actual aggregate.

 1--Oracle
 2DECLARE
 3    TYPE deptno_tbl   IS TABLE OF dept.deptno%TYPE   INDEX BY BINARY_INTEGER;
 4    TYPE dname_tbl    IS TABLE OF dept.dname%TYPE    INDEX BY BINARY_INTEGER;
 5    TYPE loc_tbl      IS TABLE OF dept.loc%TYPE      INDEX BY BINARY_INTEGER;
 6    t_deptno          deptno_TBL;
 7    t_dname           dname_TBL;
 8    t_loc             loc_TBL;
 9
10    CURSOR emp_cur IS SELECT * FROM dept;
11BEGIN
12    OPEN emp_cur;
13    FETCH emp_cur BULK COLLECT INTO t_deptno, t_dname, t_loc;
14     CLOSE emp_cur;
15     ...

FETCH BULK COLLECT INTO ROWTYPE#

Emulation of fetch bulk collect into %ROWTYPE collection is similar to regular fetch bulk collect, but we should use temporary record variable(for some reason, using .elem field as with table type doesn’t work in PG).

 1--Oracle
 2DECLARE
 3    TYPE emp_tbl IS TABLE OF emp%ROWTYPE INDEX BY BINARY_INTEGER;
 4    t_emp        emp_tbl;
 5    CURSOR emp_cur IS SELECT * FROM emp;
 6BEGIN
 7    OPEN emp_cur;
 8    FETCH emp_cur BULK COLLECT INTO t_emp;
 9
10    CLOSE emp_cur;
11    ....