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

13.1.5.2 Record-based updates

With Oracle9i Release 2, you can also perform updates of an entire row with a record. The following example inserts a row into the books table with a %ROWTYPE record. Notice that I use a new keyword, ROW, to indicate that I am updating the entire row with a record:

DECLARE

my_book books%ROWTYPE;

BEGIN

my_book.isbn := '1-56592-335-9';

my_book.title := 'ORACLE PL/SQL PROGRAMMING';

my_book.summary := 'General user guide and reference';

my_book.author := 'FEUERSTEIN, STEVEN AND BILL PRIBYL';

my_book.page_count := 980; -- new page count for 3rd edition

UPDATE books

SET ROW = my_book

WHERE isbn = my_book.isbn;

END;

There are some restrictions on record-based updates:

  • You must update an entire row with the ROW syntax. You cannot update a subset of columns (although this may be supported in future releases).

  • You cannot perform an update using a subquery.

13.1.5.3 Using records with the returning clause

DML statements can include aRETURNING clause that returns column values (and expressions based on those values) from the affected row(s). You can return into a record, or even a collection of records:

DECLARE

my_book_new_info books%ROWTYPE;

my_book_return_info books%ROWTYPE;

BEGIN

my_book.isbn := '1-56592-335-9';

my_book.title := 'ORACLE PL/SQL PROGRAMMING';

my_book.summary := 'General user guide and reference';

my_book.author := 'FEUERSTEIN, STEVEN AND BILL PRIBYL';

my_book.page_count := 980; -- new page count for 3rd edition

UPDATE books

SET ROW = my_book_new_info

WHERE isbn = my_book.isbn

RETURNING isbn, title, summary, author, page_count INTO

my_book_return_info;

END;

Notice that I must list each of my individual columns in the RETURNING clause. Oracle does not yet support use of the * syntax.

If I am updating more than one row, I can use the BULK COLLECT feature (see Chapter 14 for more information) to retrieve my RETURNING information for each row updated into a collection.

DECLARE

my_book_new_info books%ROWTYPE;

TYPE book_list_t IS TABLE OF books INDEX BY BINARY_INTEGER;

my_books book_lis_t;

BEGIN

-- The publisher switches to a new format for their Oracle books,

-- which means that all page counts will go down by 10%.

UPDATE books

SET page_count = page_count * .9

WHERE UPPER (title) LIKE '%ORACLE%'

RETURNING isbn, title, summary, author, page_count

BULK COLLECT INTO my_books;

END;

13.1.5.4 Restrictions on record-based inserts and updates

As you begin to explore these new capabilities and put them to use, keep in mind the following:

  • You can use a record variable only (a) on the right side of the SET clause in UPDATEs; (b) in the VALUES clause of an INSERT; or (c) in the INTO subclause of a RETURNING clause.

  • You must (and can only) use the ROW keyword on the left side of a SET clause. In this case, you may not have any other SET clauses (i.e., you cannot SET a row and then SET an individual column).

  • If you INSERT with a record, you cannot pass individual values for columns.

  • You cannot INSERT or UPDATE with a record that contains a nested record or with a function that returns a nested record.

  • You cannot use records in DML statements that are executed dynamically (EXECUTE IMMEDIATE). This would require Oracle to support the binding of a PL/SQL record type into a SQL statement, and only SQL types can be bound in this way.

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