Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Programming PL SQL.doc
Скачиваний:
2
Добавлен:
01.07.2025
Размер:
5.06 Mб
Скачать

11.7.1 The the Pseudo-Function

If you have a column that's a nested table and you want to insert, update, or delete from the contents of this column, you cannot do so with any SQL statement you know from "traditional" SQL. Instead, you will need to use the strangely namedkeyword THE, which helps tell Oracle which row from the outer table you want to deal with.

The THE function has been replaced in Oracle8i and above by the TABLE operator, which is discussed in the next section.

Earlier, we created the color_models table:

CREATE TABLE color_models (

model_type VARCHAR2(12),

colors Color_tab_t)

NESTED TABLE colors STORE AS color_model_colors_tab;

We had inserted a row with model_type=`RGB', and a colors column containing (`RED', `GREEN', `BLUE'). Imagine now that we've populated color_models with a half dozen or so records. One question that might have come into your mind is: how can we retrieve all of the colors for a single model using a SELECT statement?

SELECT VALUE(c) FROM

THE(SELECT colors FROM color_models

WHERE model_type = 'RGB') c;

OK, you can exhale now. The meaning of this statement is "retrieve the individual elements of the RGB color model." Or, more literally, "retrieve the value of each element of the colors nested table within the color_models outer table." Sure enough, it displays the following:

VALUE(C)

------------------------------

RED

GREEN

BLUE

I guess it's really not that weird; we're just substituting a subquery for a table in the FROM clause.

Another way you could have expressed the previous query is using the predefinedalias COLUMN_VALUE, as shown in the following example. COLUMN_VALUE is a way of referring to elements of a nested table of scalars. It is a syntactic shortcut to achieve the same result as the previous example.

SELECT COLUMN_VALUE FROM

THE(SELECT colors FROM color_models

WHERE model_type = 'RGB');

You can also use a THE subquery as the target of anINSERT, UPDATE, or DELETE statement. Here are some examples:

BEGIN

-- change BLUE to BURGUNDY inside the collection

UPDATE THE(SELECT colors FROM color_models WHERE model_type = 'RGB')

SET COLUMN_VALUE = 'BURGUNDY'

WHERE COLUMN_VALUE = 'BLUE';

-- add a silly extra color

INSERT INTO THE (

SELECT colors FROM color_models WHERE model_type = 'RGB')

VALUES ('EXTRA-COLOR');

-- show the current colors

SELECT COLUMN_VALUE

FROM THE(SELECT colors FROM color_models WHERE model_type = 'RGB');

-- delete the extra color

DELETE THE(SELECT colors FROM color_models WHERE model_type = 'RGB')

WHERE COLUMN_VALUE = 'EXTRA-COLOR';

END;

11.7.2 The table Pseudo-Function

The TABLE operator casts or converts a collection-valued column into something you can SELECT from. It sounds complicated, but this section presents an example that's not too hard to follow.

Looking at it another way, let's say that you have a database table with a column of a collection type. How can you figure out which rows in the table contain a collection that meets certain criteria? That is, how can you select from the database table, putting a WHERE clause on the collection's contents? Wouldn't it be nice if you could just say:

SELECT *

FROM table_name

WHERE collection_column

HAS CONTENTS 'whatever'; -- invalid; imaginary syntax!

Logically, that's exactly what you can do with the TABLE function. Going back to our color_models database table, how could we get a listing of all color models that contain the color RED? Here's the real way to do it:

SELECT *

FROM color_models c

WHERE 'RED' IN

(SELECT * FROM TABLE(c.colors));

which, in SQL*Plus, returns:

MODEL_TYPE COLORS

------------ ------------------------------------------------------

RGB COLOR_TAB_T('RED', 'GREEN', 'BLUE')

The query means "go through the color_models table and return all rows whose list of colors contains at least one RED element." Had there been more rows with a RED element in their colors column, these rows too would have appeared in our SQL*Plus result set.

As illustrated above, TABLE accepts an alias-qualified collection column as its argument:

TABLE(alias_name.collection_name)

TABLE returns the contents of this collection coerced into a virtual database table. Hence, you can SELECT from it. In our example, it's used in a subquery.

Does the TABLE pseudo-function remind you vaguely of the THE pseudo-function? Recall our THE example:

SELECT VALUE(c) FROM

THE(SELECT colors FROM color_models

WHERE model_type = 'RGB') c;

which returns:

VALUE(C)

------------------------------

RED

GREEN

BLUE

What is the difference between THE and TABLE? Both return something that, for purposes of the rest of the SQL statement, serves as a"virtual database table." So the difference between the functions must lie in that on which they operate—their "inputs." The TABLE function operates on a (column-typed) nested table. In contrast, the pseudo-function THE operates on a SELECT statement's result set that contains exactly one row with one column, which is a (column-typed) nested table.

As it turns out, the TABLE function gets called "under the covers" whenever you use THE as the target of an INSERT, UPDATE, or DELETE statement. This under-the-covers call coerces the results of the subquery into a virtual database table upon which the DML makes sense to operate.

To repeat an earlier admonition, none of the collection pseudo-functions is available from within PL/SQL, but PL/SQL programmers will certainly want to know how to use these gizmos in their SQL statements!

You will also find the pseudo-functions, particularly TABLE, very handy when you are taking advantage of Oracle9i's new table function capability. A table function is a function that returns a collection, and it can be used in the FROM clause of a query. This functionality is explored in Chapter 16.

Personally, I find these new features fascinating, and I enjoy the mental calisthenics required to understand and use them. Maybe mine isn't a universal sentiment, but at least you must admit that Oracle hasn't let its language technology get tired!

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]