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

Cursors

Cursors

To execute a multi-row query, Oracle opens an unnamed work area that stores processing information. A cursor lets you name the work area, access the information, and process the rows individually. For more information, see "Querying Data with PL/SQL" on page 6-9.

Syntax

cursor_declaration

 

 

 

 

,

 

 

 

 

 

 

 

 

 

 

 

(

cursor_parameter_declaration

)

CURSOR

cursor_name

 

 

 

 

 

RETURN

rowtype

 

 

 

 

 

 

 

 

 

IS

select_statement

;

 

cursor_spec

 

 

 

 

 

,

 

 

 

 

 

 

 

 

 

 

 

(

cursor_parameter_declaration

)

CURSOR

cursor_name

 

 

 

 

 

RETURN

rowtype

;

 

 

 

 

 

cursor_body

 

 

 

 

 

,

 

 

 

 

(

cursor_parameter_declaration

)

CURSOR

cursor_name

 

 

 

 

 

RETURN

rowtype

IS

select_statement

;

 

 

cursor_parameter_declaration

:=

expression

IN

DEFAULT

parameter_name

datatype

rowtype

db_table_name

cursor_name % ROWTYPE cursor_variable_name

record_name % TYPE record_type_name

13-38 PL/SQL User's Guide and Reference

Cursors

Keyword and Parameter Description

cursor_name

An explicit cursor previously declared within the current scope.

datatype

A type specifier. For the syntax of datatype, see "Constants and Variables" on page 13-28.

db_table_name

A database table or view that must be accessible when the declaration is elaborated.

expression

A combination of variables, constants, literals, operators, and function calls. The simplest expression consists of a single variable. When the declaration is elaborated, the value of expression is assigned to the parameter. The value and the parameter must have compatible datatypes.

parameter_name

A variable declared as the formal parameter of a cursor. A cursor parameter can appear in a query wherever a constant can appear. The formal parameters of a cursor must be IN parameters. The query can also reference other PL/SQL variables within its scope.

record_name

A user-defined record previously declared within the current scope.

record_type_name

A user-defined record type that was defined using the datatype specifier RECORD.

RETURN

Specifies the datatype of a cursor return value. You can use the %ROWTYPE attribute in the RETURN clause to provide a record type that represents a row in a database table or a row returned by a previously declared cursor. Also, you can use the %TYPE attribute to provide the datatype of a previously declared record.

A cursor body must have a SELECT statement and the same RETURN clause as its corresponding cursor spec. Also, the number, order, and datatypes of select items in the SELECT clause must match the RETURN clause.

%ROWTYPE

A record type that represents a row in a database table or a row fetched from a previously declared cursor or cursor variable. Fields in the record and corresponding columns in the row have the same names and datatypes.

select_statement

A query that returns a result set of rows. Its syntax is like that of select_into_statement without the INTO clause. See "SELECT INTO Statement" on page 13-123. If the cursor declaration declares parameters, each parameter must be used in the query.

PL/SQL Language Elements 13-39

Cursors

%TYPE

Provides the datatype of a previously declared user-defined record.

Usage Notes

You must declare a cursor before referencing it in an OPEN, FETCH, or CLOSE statement. You must declare a variable before referencing it in a cursor declaration. The word SQL is reserved by PL/SQL as the default name for implicit cursors, and cannot be used in a cursor declaration.

You cannot assign values to a cursor name or use it in an expression. However, cursors and variables follow the same scoping rules. For more information, see "Scope and Visibility of PL/SQL Identifiers" on page 2-14.

You retrieve data from a cursor by opening it, then fetching from it. Because the FETCH statement specifies the target variables, using an INTO clause in the SELECT statement of a cursor_declaration is redundant and invalid.

The scope of cursor parameters is local to the cursor, meaning that they can be referenced only within the query used in the cursor declaration. The values of cursor parameters are used by the associated query when the cursor is opened. The query can also reference other PL/SQL variables within its scope.

The datatype of a cursor parameter must be specified without constraints, that is, without precision and scale for numbers, and without length for strings.

Examples

Some examples of cursor declarations follow:

CURSOR c1

IS SELECT empno, ename, job, sal FROM emp

WHERE sal > 2000;

CURSOR c2

RETURN

dept%ROWTYPE IS

SELECT

* FROM

dept WHERE deptno = 10;

CURSOR c3

(start_date DATE) IS

SELECT

empno,

sal FROM emp WHERE hiredate > start_date;

Related Topics

CLOSE Statement, FETCH Statement, OPEN Statement, SELECT INTO Statement

13-40 PL/SQL User's Guide and Reference

DELETE Statement

DELETE Statement

The DELETE statement removes entire rows of data from a specified table or view. For a full description of the DELETE statement, see Oracle Database SQL Reference.

Syntax

delete_statement

 

 

 

 

 

 

 

 

 

FROM

table_reference

 

 

alias

 

 

 

 

 

 

 

 

 

DELETE

 

(

subquery

)

 

 

 

 

 

 

TABLE

(

subquery2

)

 

 

 

 

search_condition

 

 

 

 

 

 

 

WHERE

 

 

 

 

 

 

 

 

 

 

CURRENT OF

 

cursor_name

 

returning_clause

 

 

 

 

 

 

 

 

 

 

;

 

table_reference

 

 

 

 

 

 

 

 

 

schema_name

.

db_table_name

@

 

dblink_name

 

 

 

 

view_name

 

 

 

 

 

 

returning_clause

 

 

 

 

 

 

,

 

 

 

,

 

 

 

 

 

 

 

 

 

 

 

 

 

 

variable_name

 

 

 

single_row_expression

 

INTO

 

 

 

 

 

 

 

 

 

 

:

host_variable_name

 

 

RETURNING

 

 

 

 

 

 

 

 

,

 

 

,

 

 

BULK COLLECT

 

 

 

 

 

 

 

collection_name

 

multiple_row_expression

 

 

 

INTO

 

 

 

 

 

 

 

 

 

 

:

host_array_name

Keyword and Parameter Description

alias

Another (usually short) name for the referenced table or view. Typically referred to later in the WHERE clause.

BULK COLLECT

Returns columns from the deleted rows into PL/SQL collections, as specified by the RETURNING INTO list. The corresponding columns must store scalar (not composite) values. For more information, see "Reducing Loop Overhead for DML Statements and Queries (FORALL, BULK COLLECT)" on page 11-7.

returning_clause

Returns values from the deleted rows, eliminating the need to SELECT the rows first. You can retrieve the column values into individual variables or into collections. You cannot use the RETURNING clause for remote or parallel deletes. If the statement does not affect any rows, the values of the variables specified in the RETURNING clause are undefined.

PL/SQL Language Elements 13-41

DELETE Statement

subquery

A SELECT statement that provides a set of rows for processing. Its syntax is like the select_into_statement without the INTO clause. See "SELECT INTO Statement" on page 13-123.

table_reference

A table or view, which must be accessible when you execute the DELETE statement, and for which you must have DELETE privileges.

TABLE (subquery2)

The operand of TABLE is a SELECT statement that returns a single column value, which must be a nested table. Operator TABLE informs Oracle that the value is a collection, not a scalar value.

WHERE CURRENT OF cursor_name

Refers to the latest row processed by the FETCH statement associated with the cursor identified by cursor_name. The cursor must be FOR UPDATE and must be open and positioned on a row. If the cursor is not open, the CURRENT OF clause causes an error.

If the cursor is open, but no rows have been fetched or the last fetch returned no rows, PL/SQL raises the predefined exception NO_DATA_FOUND.

WHERE search_condition

Conditionally chooses rows to be deleted from the referenced table or view. Only rows that meet the search condition are deleted. If you omit the WHERE clause, all rows in the table or view are deleted.

Usage Notes

You can use the DELETE WHERE CURRENT OF statement after a fetch from an open cursor (this includes implicit fetches executed in a cursor FOR loop), provided the associated query is FOR UPDATE. This statement deletes the current row; that is, the one just fetched.

The implicit cursor SQL and the cursor attributes %NOTFOUND, %FOUND, and %ROWCOUNT let you access useful information about the execution of a DELETE statement.

Examples

The following statement deletes the rows that match a condition:

DELETE FROM bonus WHERE sales_amt < quota;

The following statement returns two column values from a deleted row into local variables:

DECLARE

my_empno emp.empno%TYPE; my_ename emp.ename%TYPE; my_job emp.job%TYPE;

BEGIN

...

DELETE FROM emp WHERE empno = my_empno RETURNING ename, job INTO my_ename, my_job;

END;

13-42 PL/SQL User's Guide and Reference

DELETE Statement

You can combine the BULK COLLECT clause with a FORALL statement, in which case, the SQL engine bulk-binds column values incrementally. In the following example, if collection depts has 3 elements, each of which causes 5 rows to be deleted, then collection enums has 15 elements when the statement completes:

FORALL j IN depts.FIRST..depts.LAST

DELETE FROM emp WHERE deptno = depts(j)

RETURNING empno BULK COLLECT INTO enums;

The column values returned by each execution are added to the values returned previously.

Related Topics

FETCH Statement, INSERT Statement, SELECT INTO Statement, UPDATE Statement

PL/SQL Language Elements 13-43

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