Skip to content

Commit

Permalink
minor edits done to FETCH BULK COLLECT INTO topic
Browse files Browse the repository at this point in the history
  • Loading branch information
nidhibhammar authored and drothery-edb committed Aug 8, 2023
1 parent 7ad6d1e commit 06fb92b
Showing 1 changed file with 6 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,34 @@ redirects:

<div id="fetch_bulk_collect" class="registered_link"></div>

You can use the `BULK COLLECT` clause with a `FETCH` statement. Instead of returning a single row at a time from the result set, the `FETCH BULK COLLECT` returns all rows at once from the result set into one or more specified collections unless restricted by the `LIMIT` clause:
You can use the `BULK COLLECT` clause with a `FETCH` statement. Instead of returning a single row at a time from the result set, the `FETCH BULK COLLECT` returns all rows at once from the result set into one or more specified collections (both scalar and composite types) unless restricted by the `LIMIT` clause:

```sql
FETCH <name> BULK COLLECT INTO <collection> [, ...] [ LIMIT <n> ];
```

For information on the `FETCH` statement, see [Fetching rows from a cursor](../../08_static_cursors/03_fetching_rows_from_a_cursor/#fetching_rows_from_a_cursor).

If you specify a single collection, then `collection` can be a collection of a single field, or it can be a collection of a record type. If you specify more than one collection, then each `collection` must consist of a single field or a row-type. The expressions in the `SELECT` list of the cursor identified by `name` must match all fields in the target collections in number, order, and type-compatibility. If you specify `LIMIT n`, the number of rows returned into the collection on each `FETCH` doesn't exceed `n`.
If you specify a single collection, then `collection` can be a collection of a single field, or it can be a collection of a record type. If you specify more than one collection, then each `collection` must consist of a single field or a record type. The expressions in the `SELECT` list of the cursor identified by `name` must match all fields in the target collections in number, order, and type-compatibility. If you specify `LIMIT n`, the number of rows returned into the collection on each `FETCH` doesn't exceed `n`.

This example uses the `FETCH BULK COLLECT` statement to retrieve rows into an associative array:

```sql
-- Create two object types of composite data types
CREATE TYPE db_type1 as OBJECT(a INT, b VARCHAR2(10));
CREATE TYPE db_type2 as OBJECT(c VARCHAR2(10), d INT);

-- Create a table using above object types
CREATE TABLE db_tab(x DB_TYPE1, s1 NUMBER, y DB_TYPE2, s2 TIMESTAMP);

-- Insert the rows into the table
INSERT INTO db_tab values(DB_TYPE1(1, '10'), 1.1, DB_TYPE2('100',1000), '1-Jan-2021 12:30:11 PM');
INSERT INTO db_tab values(DB_TYPE1(2, '20'), 2.2, DB_TYPE2('200',2000), '2-Feb-2022 08:40:52 AM');
INSERT INTO db_tab values(DB_TYPE1(3, '30'), 3.3, DB_TYPE2('300',3000), '3-Jan-2023 04:20:33 PM');
```

```sql
-- Use FETCH BULK COLLECT INTO clause for fetching both scalar and composite types
DECLARE
TYPE type1_tbl IS TABLE OF db_type1 INDEX BY BINARY_INTEGER;
TYPE s1_tbl IS TABLE OF number INDEX BY BINARY_INTEGER;
Expand Down

0 comments on commit 06fb92b

Please sign in to comment.