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

11.7.5 Sorting Contents of Collections

One of the wonderful aspects of pseudo-functions is that you can apply SQL operations against the contents of PL/SQL data structures (nested tables and VARRAYs, at least). You can, for example, use ORDER BY to select information from the nested table in the order you desire. Here, I populate a database table with some of my favorite authors:

CREATE TYPE names_t AS TABLE OF VARCHAR2 (100);

CREATE TYPE authors_t AS TABLE OF VARCHAR2 (100);

CREATE TABLE favorite_authors (name varchar2(200))

BEGIN

INSERT INTO favorite_authors VALUES ('Robert Harris');

INSERT INTO favorite_authors VALUES ('Tom Segev');

INSERT INTO favorite_authors VALUES ('Toni Morrison');

END;

Now I would like to blend this information with data from my PL/SQL program:

DECLARE

scifi_favorites authors_t

:= authors_t ('Sheri S. Tepper', 'Orson Scott Card', 'Gene Wolfe');

BEGIN

DBMS_OUTPUT.put_line ('I recommend that you read books by:');

FOR rec IN (SELECT column_value favs

FROM TABLE (cast (scifi_favorites AS names_t))

UNION

SELECT NAME

FROM favorite_authors)

LOOP

DBMS_OUTPUT.put_line (rec.favs);

END LOOP;

END;

Notice that I can use UNION to combine data from my database table and collection. I can also apply this technique only to PL/SQL data to sort the contents being retrieved:

DECLARE

scifi_favorites authors_t

:= authors_t ('Sheri S. Tepper', 'Orson Scott Card', 'Gene Wolfe');

BEGIN

DBMS_OUTPUT.put_line ('I recommend that you read books by:');

FOR rec IN (SELECT column_value favs

FROM TABLE (cast (scifi_favorites AS names_t))

ORDER BY column_value)

LOOP

DBMS_OUTPUT.put_line (rec.favs);

END LOOP;

END;

11.8 Maintaining Collections

Here are some not-so-obvious bits of information that will assist you in using nested tables and VARRAYS. This sort of housekeeping is not necessary or relevant when working with associative arrays.

11.8.1 Privileges

When they live in the database, collection datatypes can be shared by more than one Oracle user (schema). As you can imagine, privileges are involved. Fortunately, it's not complicated; only one Oracle privilege—EXECUTE—applies to collection types.

If you are Scott and you want to grant Joe permission to use Color_tab_t in his programs, all you need to do is grant the EXECUTE privilege to him:

GRANT EXECUTE on Color_tab_t TO JOE;

Joe can then refer to the type using schema.type notation. For example:

CREATE TABLE my_stuff_to_paint (

which_stuff VARCHAR2(512),

paint_mixture SCOTT.Color_tab_t

)

NESTED TABLE paint_mixture STORE AS paint_mixture_st;

EXECUTE privileges are also required by users who need to run PL/SQL anonymous blocks that use the object type. That's one of several reasons that named PL/SQL modules—packages, procedures, functions—are generally preferred. Granting EXECUTE on the module confers the grantor's privileges to the grantee while executing the module.

For tables that include collection columns, the traditional SELECT, INSERT, UDPATE, and DELETE privileges still have meaning, as long as there is no requirement to build a collection for any columns. However, if a user is going to INSERT or UPDATE the contents of a collection column, that user must have the EXECUTE privilege on the type because that is the only way to use the default constructor.

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