FETCH cursor BULK COLLECT : Cursor Fetch « Cursor « Oracle PL / SQL






FETCH cursor BULK COLLECT

  
SQL>
SQL> CREATE TABLE MyTable (
  2    num_col    NUMBER,
  3    char_col   VARCHAR2(60));

Table created.

SQL>
SQL> set serveroutput on format wrapped
SQL>
SQL> DECLARE
  2    TYPE t_Numbers IS TABLE OF MyTable.num_col%TYPE;
  3    TYPE t_Strings IS TABLE OF MyTable.char_col%TYPE;
  4    v_Numbers t_Numbers := t_Numbers(1);
  5    v_Strings t_Strings := t_Strings(1);
  6    v_Numbers2 t_Numbers;
  7    v_Strings2 t_Strings;
  8
  9    CURSOR c_char IS
 10      SELECT char_col
 11      FROM MyTable
 12      WHERE num_col > 800
 13      ORDER BY num_col;
 14
 15  BEGIN
 16    v_Numbers.EXTEND(1500);
 17    v_Strings.EXTEND(1500);
 18    FOR v_Count IN 1..1000 LOOP
 19      v_Numbers(v_Count) := v_Count;
 20      v_Strings(v_Count) := 'Element #' || v_Count;
 21      IF v_Count > 500 THEN
 22        v_Numbers(v_Count + 500) := v_Count;
 23        v_Strings(v_Count + 500) := 'Element #' || v_Count;
 24      END IF;
 25    END LOOP;
 26
 27    DELETE FROM MyTable;
 28    FORALL v_Count IN 1..1500
 29      INSERT INTO MyTable (num_col, char_col)
 30        VALUES (v_Numbers(v_Count), v_Strings(v_Count));
 31
 32    SELECT num_col, char_col
 33      BULK COLLECT INTO v_Numbers, v_Strings
 34      FROM MyTable
 35      ORDER BY num_col;
 36
 37    DBMS_OUTPUT.PUT_LINE('First query fetched ' || v_Numbers.COUNT || ' rows');
 38
 39    SELECT num_col
 40      BULK COLLECT INTO v_Numbers2
 41      FROM MyTable;
 42
 43    DBMS_OUTPUT.PUT_LINE('Second query fetched ' || v_Numbers2.COUNT || ' rows');
 44
 45    OPEN c_char;
 46    FETCH c_char BULK COLLECT INTO v_Strings2;
 47    CLOSE c_char;
 48
 49    DBMS_OUTPUT.PUT_LINE('Cursor fetch retrieved ' || v_Strings2.COUNT || ' rows');
 50
 51  END;
 52  /
First query fetched 1500 rows
Second query fetched 1500 rows
Cursor fetch retrieved 400 rows

PL/SQL procedure successfully completed.

SQL>
SQL>
SQL> drop table MyTable;

Table dropped.

SQL>
SQL>

   
  








Related examples in the same category

1.Below is a function that demonstrates how to use the FETCH statement
2.Fetch value from cursor
3.Fetches the records from a cursor variable that returns employee information
4.Fetch cursor to two variables
5.legal and illegal FETCH statements.
6."ORA-1002: fetch out of sequence" because of the commit inside the SELECT..FOR UPDATE loop.
7.A WHILE cursor fetch loop.
8.Different BULK_COLLECT statements used for bulk binds
9.Check Cursor%FOUND after fetching
10.Fetch value in cursor until NOTFOUND
11.ORA-01002: fetch out of sequence
12.Fetch out cursor value and insert to another table
13.PLS-00394: wrong number of values in the INTO list of a FETCH statement
14.Fetch cursor data to number variable
15.Fetch cursor till cursorVariable%notfound
16.Fetch cursor to table collection of row type
17.Fetch cursor value to three variables
18.Fetch row by row
19.Fetch single column cursor to varchar2 variable