Collections in PL/SQL

A collection is a datatype that offers a way to store singly dimensioned arrays in PL/SQL. Great. So what does that mean? You will use collections to create lists of related information, either in your PL/SQL program or in the column of a database table. Here are some of the ways we’ve found collections handy:

  • Emulate bidirectional or random-accesscursors. PL/SQL only allows you to fetch forward through a cursor’s result set. But if I load the result set of a cursor into a collection, I can move back and forth through that set, and can instantly (and repetitively) access any particular row in the set.

  • Improve performance of lookups by storing lists of subordinate information directly in the column of a table (as a nested table or VARRAY), rather than normalizing that data into a separate relational table. (Nested tables, VARRAYs, and associative arrays are collection types described in the upcoming section Section 11.2.2.)

  • Keep track of data elements selected in a program for special processing.

  • Cache database information that is static and frequently queried to speed up performance of those queries.

In the following sections we’ll show how to create and use collection types both in the database and in PL/SQL programs, and show the syntax for creating collection types. We’ll present the three different initialization techniques with additional examples, and review the built-in “methods,” such as NEXT, DELETE, and TRIM, for managing collection content. Although ...

Get Oracle PL/SQL Programming, Third Edition now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.