In the previous blog post, I have given an example to use Oracle Bulk Collect Using Cursor Rowtype Type Object. Which first creates the cursor then type object of cursor type and then open the cursor, fetching into bulk collect and then finally process them using Forall. But sometimes you may be required to prepare some data for just one time with a temporary anonymous PLSQL block, and you don't want to write too much code to perform that task. For this kind of scenarios, you can use Select Bulk Collect into an option to perform any task quickly without wasting too much time to write code.
Below is the example of PLSQL block which will take the records from EMP table and update the BONUS table and also prints on the screen.
SET SEVEROUTPUT ON; DECLARE TYPE t_emp IS TABLE OF emp%ROWTYPE; e_rec t_emp; BEGIN SELECT * BULK COLLECT INTO e_rec FROM emp; FORALL i IN e_rec.FIRST .. e_rec.LAST update bonus set amount = e_rec(i).sal * 15 / 100 where empno = e_rec(i).empno; Commit; FOR i IN e_rec.FIRST .. e_rec.LAST LOOP DBMS_OUTPUT. put_line ( 'Bonus Updated For Employee: ' || e_rec (i).ename || ' ' || 'Bonus: ' || e_rec(i).sal * 15 / 100); END LOOP; END;