Working with Collections

By now, you have learned how to define a TYPE of collection, whether in the database or in your code base. You also know how to declare collections based on those types. Now let’s examine the kind of code you need to write to actually work with those collections. There are three main programming tasks you need to understand:

  • How to properly initialize the collection variable

  • How to assign values to elements without raising exceptions

  • How to add and remove “slots” for elements

In addition, to fully exploit the programming utility of collections, you need to learn how to retrieve and store sets of data with them. This leads into our section on pseudo-functions, which allow you to perform magic tricks with collections. (OK, maybe it’s not real magic, but you’re almost guaranteed to say “How did they do that?” the first time you try to program this stuff.)

Initializing Collection Variables

With an associative array datatype, initialization is a nonissue. Simply declaring an associative array variable initializes it in an “empty” state. Then you can just assign values to subscripted table elements as you desire. Index values (subscripts) can be almost any positive or negative integer. A program can even assign subscripts to associative arrays arbitrarily, skipping huge ranges of subscripts without paying a memory or performance penalty.[14] So if you are working with associative arrays, you can skip to the next section on assigning elements to a collection. ...

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

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.