10.4 SQL Operations on CollectionsIn Oracle 8.1, you can now more easily use SQL to operate on PL/SQL collections (nested tables and variable arrays). This feature, built upon the CAST operator, allows you to employ SQL capabilities against data stored in such structures, and also to integrate data in these collections with data in tables. Here is the syntax needed to reference a collection inside a query:
Where collection is a collection declared in a PL/SQL block, collection_type is the TYPE from which the collection is declared, and collection_alias is an optional alias for the collection-cast-into-table. column_list is a list of expressions returned by the query. If the collection TYPE is a nested table or variable array based on a scalar, then column_list must be the keyword COLUMN_VALUE, as shown: SELECT COLUMN_VALUE FROM TABLE (CAST ...); If the collection is based on an object, then the column list can directly reference individual elements of the object by name. Let's look at a few examples. I will create a type of nested table and a database table to use in the scripts (see collsql.sql for the full set of steps):
Then I can merge the data for these two structures together, as follows:
And out comes the following data: How to Make the NYC Rich Much, Much Richer: Close public hospitals Cut city income taxes Fire building inspectors No tax on stock transactions Stop rat extermination programs As you can see, the data is sorted, as will happen automatically with a UNION. Now let's try this capability with a nested table of objects (see collsql2.sql ): CREATE TYPE labor_source AS OBJECT (labor_type VARCHAR2(30), hourly_rate NUMBER); / CREATE TYPE union_busters AS TABLE OF labor_source; / In the following block, I query both members of the object individually, employing an ORDER BY clause to rearrange the data:
Prisoner-$5 Workfare-$0
Copyright (c) 2000 O'Reilly & Associates. All rights reserved. |
|