Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Скачиваний:
51
Добавлен:
16.04.2013
Размер:
5.97 Mб
Скачать

Performing DML Operations from PL/SQL (INSERT, UPDATE, and DELETE)

Row Operators

Row operators return or reference particular rows. ALL retains duplicate rows in the result of a query or in an aggregate expression. DISTINCT eliminates duplicate rows from the result of a query or from an aggregate expression. PRIOR refers to the parent row of the current row returned by a tree-structured query.

Performing DML Operations from PL/SQL (INSERT, UPDATE, and DELETE)

You can write INSERT, UPDATE, and DELETE statements directly in PL/SQL programs, without any special notation:

CREATE table1 AS SELECT object_name, object_type FROM user_objects;

BEGIN

INSERT INTO table1(col1, col2) VALUES('value1','value2'); UPDATE table1 SET col1 = 'another value' WHERE col2 IS NULL; DELETE FROM table1 WHERE col1 = col2;

COMMIT;

END;

/

DROP table1;

To find out how many rows are affected by these statements, you can check the value of SQL%ROWCOUNT:

SET SERVEROUTPUT ON; BEGIN

UPDATE employees SET salary = salary * 1.05 WHERE ...; dbms_output.put_line('Updated ' || SQL%ROWCOUNT || ' salaries.');

END;

/

Wherever you would use literal values, or bind variables in some other programming language, you can directly substitute PL/SQL variables:

CREATE table1 AS SELECT object_name, object_type FROM user_objects;

DECLARE

x VARCHAR2(128) := 'value1'; y NUMBER := 10;

BEGIN

INSERT INTO table1(col1, col2) VALUES(x, x); UPDATE table1 SET col1 = x WHERE col3 < y; DELETE FROM table1 WHERE col1 = x;

COMMIT;

END;

/

DROP table1;

With this notation, you can use variables in place of values in the WHERE clause. To use variables in place of table names, column names, and so on, requires the EXECUTE IMMEDIATE statement that is explained in ...

Performing SQL Operations from PL/SQL 6-5

Performing DML Operations from PL/SQL (INSERT, UPDATE, and DELETE)

Overview of Implicit Cursor Attributes

Implicit cursor attributes return information about the execution of an INSERT, UPDATE, DELETE, or SELECT INTO statement. The values of the cursor attributes always refer to the most recently executed SQL statement. Before Oracle opens the SQL cursor, the implicit cursor attributes yield NULL.

Note: The SQL cursor has another attribute, %BULK_ROWCOUNT, designed for use with the FORALL statement. For more information, see "Counting Rows Affected by FORALL with the %BULK_ROWCOUNT Attribute" on page 11-12.

%FOUND Attribute: Has a DML Statement Changed Rows?

Until a SQL data manipulation statement is executed, %FOUND yields NULL. Thereafter, %FOUND yields TRUE if an INSERT, UPDATE, or DELETE statement affected one or more rows, or a SELECT INTO statement returned one or more rows. Otherwise, %FOUND yields FALSE. In the following example, you use %FOUND to insert a row if a delete succeeds:

DELETE FROM emp WHERE empno = my_empno;

IF SQL%FOUND THEN -- delete succeeded

INSERT INTO new_emp VALUES (my_empno, my_ename, ...);

%ISOPEN Attribute: Always FALSE for Implicit Cursors

Oracle closes the SQL cursor automatically after executing its associated SQL statement. As a result, %ISOPEN always yields FALSE.

%NOTFOUND Attribute: Has a DML Statement Failed to Change Rows?

%NOTFOUND is the logical opposite of %FOUND. %NOTFOUND yields TRUE if an INSERT,

UPDATE, or DELETE statement affected no rows, or a SELECT INTO statement returned no rows. Otherwise, %NOTFOUND yields FALSE.

%ROWCOUNT Attribute: How Many Rows Affected So Far?

%ROWCOUNT yields the number of rows affected by an INSERT, UPDATE, or DELETE statement, or returned by a SELECT INTO statement. %ROWCOUNT yields 0 if an INSERT, UPDATE, or DELETE statement affected no rows, or a SELECT INTO statement returned no rows. In the following example, you use %ROWCOUNT to take action if more than ten rows have been deleted:

DELETE FROM emp WHERE ...

IF SQL%ROWCOUNT > 10 THEN -- more than 10 rows were deleted

...

END IF;

If a SELECT INTO statement returns more than one row, PL/SQL raises the predefined exception TOO_MANY_ROWS and %ROWCOUNT yields 1, not the actual number of rows that satisfy the query.

Guidelines for Using Implicit Cursor Attributes

The values of the cursor attributes always refer to the most recently executed SQL statement, wherever that statement is. It might be in a different scope (for example, in a sub-block). To save an attribute value for later use, assign it to a Boolean variable immediately. Doing other operations, such as procedure calls, might change the value of %NOTFOUND before you can test it.

The %NOTFOUND attribute is not useful in combination with the SELECT INTO statement:

6-6 PL/SQL User's Guide and Reference

Соседние файлы в папке Oracle 10g