4. FORALL 的属性
执行 FORALL 后,你可以使用游标属性来获取 DML 语句的执行信息。除此以外,还可以使用 %BULK_ROWCOUNT
属性来获取更细粒度的执行信息。下面是FORALL中的 DML 语句的隐式 SQL 游标属性:
|
Name |
Description |
|
SQL%FOUND |
Returns TRUE if the last
execution of the SQL statement modified one or more rows. |
|
SQL%NOTFOUND |
Returns FALSE if the last
execution of the SQL statement modified one or more rows. |
|
SQL%ROWCOUNT |
Returns the total number of rows processed by all
executions of the SQL statement, not just the last
statement. |
|
SQL%ISOPEN |
Always returns FALSE and should not be
used. |
|
SQL%BULK_ROWCOUNT |
Returns a collection that tells you the number of
rows processed by each corresponding SQL statement executed via FORALL. Note:
when %BULK_ROWCOUNT(i) is zero, %FOUND and
%NOTFOUND are FALSE and TRUE,
respectively. |
我们看一下 %BULK_ROWCOUNT,它是为
FORALL 而设计的。它使用起来就像一个伪集合,在第 N 个元素中,保存的是 FORALL 的第 N 个 DML 语句处理的行数。如果没有处理任何行,则第 N
个元素中保存的值是 0。下面我们看一个例子,它使用到了 %BULK_ROWCOUNT,同时也用到了 %ROWCOUNT:
|
DECLARE TYPE isbn_list IS TABLE OF VARCHAR2(13);
my_books isbn_list := isbn_list (
'1-56592-375-8', '0-596-00121-5',
'1-56592-849-0', '1-56592-335-9',
'1-56592-674-9', '1-56592-675-7',
'0-596-00180-0', '1-56592-457-6' ); BEGIN FORALL
book_index IN my_books.FIRST..my_books.LAST UPDATE books
SET page_count WHERE isbn = my_books (book_index);
-- Did I update the total number of books I
expected? IF SQL%ROWCOUNT != 8 THEN DBMS_OUTPUT.PUT_LINE
('We are missing a book!'); END IF;
-- Did the 4th UPDATE statement affect any
rows? IF SQL%BULK_ROWCOUNT(4) = 0 THEN
DBMS_OUTPUT.PUT_LINE ('What happened to Oracle PL/SQL Programming?');; END
IF; END; |
关于 %BULK_ROWCOUNT,需要注意以下几点:
* FORALL 语句和 %BULK_ROWCOUNT 具有相同数量的元素,并使用相同的下标。例如 FORALL 中使用 10-200
的下标处理集合,那么返回的 %BULK_ROWCOUNT 伪集合的下标也是 10-200。
* When the INSERT affects only a single row (when you specify a VALUES
list, for example), a row's value in %BULK_ROWCOUNT will be equal to 1. For
INSERT...SELECT statements, however, %BULK_ROWCOUNT can be greater than 1.
* DELETE 和 INSERT 操作所产生的伪集合 %BULK_ROWCOUNT 的行数可以是任何自然数(0或正数),取决于它们的 WHERE
条件所影响的行数。