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

OPEN-FOR-USING Statement

OPEN-FOR-USING Statement

The OPEN-FOR-USING statement associates a cursor variable with a query, executes the query, identifies the result set, positions the cursor before the first row in the result set, then zeroes the rows-processed count kept by %ROWCOUNT. For more information, see "Building a Dynamic Query with Dynamic SQL" on page 7-4.

Because this statement can use bind variables to make the SQL processing more efficient, use the OPEN-FOR-USING statement when building a query where you know the WHERE clauses in advance. Use the OPEN-FOR statement when you need the flexibility to process a dynamic query with an unknown number of WHERE clauses.

Syntax

open_for_using_statement

cursor_variable_name

OPEN FOR dynamic_string : host_cursor_variable_name

,

USING bind_argument

;

Keyword and Parameter Description

cursor_variable_name

A weakly typed cursor variable (one without a return type) previously declared within the current scope.

bind_argument

An expression whose value is passed to the dynamic SELECT statement.

dynamic_string

A string literal, variable, or expression that represents a multi-row SELECT statement.

host_cursor_variable_name

A cursor variable declared in a PL/SQL host environment and passed to PL/SQL as a bind variable. The datatype of the host cursor variable is compatible with the return type of any PL/SQL cursor variable. Host variables must be prefixed with a colon.

USING ...

This optional clause specifies a list of bind arguments. At run time, bind arguments in the USING clause replace corresponding placeholders in the dynamic SELECT statement.

Usage Notes

You use three statements to process a dynamic multi-row query: OPEN-FOR-USING, FETCH, and CLOSE. First, you OPEN a cursor variable FOR a multi-row query. Then, you FETCH rows from the result set. When all the rows are processed, you CLOSE the cursor variable.

PL/SQL Language Elements 13-97

OPEN-FOR-USING Statement

The dynamic string can contain any multi-row SELECT statement (without the terminator). The string can also contain placeholders for bind arguments. However, you cannot use bind arguments to pass the names of schema objects to a dynamic SQL statement.

Every placeholder in the dynamic string must be associated with a bind argument in the USING clause. Numeric, character, and string literals are allowed in the USING clause, but Boolean literals (TRUE, FALSE, NULL) are not. To pass nulls to the dynamic string, you must use a workaround. See "Passing Nulls to Dynamic SQL" on page 7-10.

Any bind arguments in the query are evaluated only when the cursor variable is opened. To fetch from the cursor using different bind values, you must reopen the cursor variable with the bind arguments set to their new values.

Dynamic SQL supports all the SQL datatypes. For example, bind arguments can be collections, LOBs, instances of an object type, and refs. As a rule, dynamic SQL does not support PL/SQL-specific types. For instance, bind arguments cannot be Booleans or index-by tables.

Example

The following example declares a cursor variable, then associates it with a dynamic SELECT statement:

DECLARE

TYPE EmpCurTyp IS REF CURSOR; -- define weak REF CURSOR type

emp_cv

EmpCurTyp; -- declare cursor variable

my_ename

VARCHAR2(15);

my_sal

NUMBER := 1000;

BEGIN

 

OPEN emp_cv FOR -- open cursor variable

'SELECT ename, sal FROM emp WHERE sal > :s' USING my_sal;

...

END;

Related Topics

EXECUTE IMMEDIATE Statement, OPEN-FOR Statement

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

Packages

Packages

A package is a schema object that groups logically related PL/SQL types, items, and subprograms. Use packages when writing a set of related subprograms that form an application programming interface (API) that you or others might reuse. Packages have two parts: a specification (spec for short) and a body. For more information, see Chapter 9, "Using PL/SQL Packages".

Syntax

package_declaration | package_spec

 

 

 

 

OR REPLACE

 

schema_name

.

 

CREATE

PACKAGE

 

package_name

 

CURRENT_USER

 

 

 

 

AUTHID

 

 

 

 

DEFINER

IS

PRAGMA SERIALLY_REUSABLE

;

AS

collection_type_definition record_type_definition subtype_definition collection_declaration constant_declaration exception_declaration

object_declaration

package_name

END

;

record_declaration

 

variable_declaration

 

cursor_spec

 

function_spec

 

procedure_spec

 

call spec

 

pragma_restrict_refs

 

PL/SQL Language Elements 13-99

Packages

package_body

 

 

 

 

 

OR REPLACE

 

schema_name

.

CREATE

PACKAGE BODY

 

package_name

IS

PRAGMA SERIALLY_REUSABLE

;

 

 

AS

 

 

 

 

collection_type_definition record_type_definition subtype_definition collection_declaration constant_declaration exception_declaration

BEGIN

statement

 

package_name

object_declaration

 

END

;

record_declaration

 

 

 

variable_declaration

 

 

 

cursor_body

 

 

 

function_body

 

 

 

procedure_body

 

 

 

call spec

 

 

 

Keyword and Parameter Description

AUTHID

Determines whether all the packaged subprograms execute with the privileges of their definer (the default) or invoker, and whether their unqualified references to schema objects are resolved in the schema of the definer or invoker. For more information, see "Using Invoker's Rights Versus Definer's Rights (AUTHID Clause)" on page 8-15.

call_spec

Publishes a Java method or external C function in the Oracle data dictionary. It publishes the routine by mapping its name, parameter types, and return type to their SQL counterparts. For more information, see Oracle Database Java Developer's Guide and

Oracle Database Application Developer's Guide - Fundamentals.

collection_declaration

Declares a collection (nested table, index-by table, or varray). For the syntax of collection_declaration, see "Collections" on page 13-21.

collection_type_definition

Defines a collection type using the datatype specifier TABLE or VARRAY.

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

Packages

constant_declaration

Declares a constant. For the syntax of constant_declaration, see "Constants and Variables" on page 13-28.

cursor_body

Defines the underlying implementation of an explicit cursor. For the syntax of cursor_body, see "Cursors" on page 13-38.

cursor_spec

Declares the interface to an explicit cursor. For the syntax of cursor_spec, see "Cursors" on page 13-38.

exception_declaration

Declares an exception. For the syntax of exception_declaration, see "Exceptions" on page 13-45.

function_body

Implements a function. For the syntax of function_body, see "Functions" on page 13-67.

function_spec

Declares the interface to a function. For the syntax of function_spec, see "Functions" on page 13-67.

object_declaration

Declares an object (instance of an object type). For the syntax of object_declaration, see "Object Types" on page 13-86.

package_name

A package stored in the database. For naming conventions, see "Identifiers" on page 2-3.

pragma_restrict_refs

Pragma RESTRICT_REFERENCES, which checks for violations of "purity" rules. To be callable from SQL statements, a function must obey rules that control side effects. If any SQL statement inside the function body violates a rule, you get an error at run time (when the statement is parsed). For the syntax of the pragma, see "RESTRICT_REFERENCES Pragma" on page 13-113.

The pragma asserts that a function does not read and/or write database tables and/or package variables. For more information about the purity rules and pragma

RESTRICT_REFERENCES, see Oracle Database Application Developer's Guide - Fundamentals.

PRAGMA SERIALLY_REUSABLE

Marks a package as serially reusable, if its state is needed only for the duration of one call to the server (for example, an OCI call to the server or a server-to-server remote procedure call). For more information, see Oracle Database Application Developer's Guide - Fundamentals.

PL/SQL Language Elements 13-101

Packages

procedure_body

Implements a procedure. For the syntax of procedure_body, see "Procedures" on page 13-104.

procedure_spec

Declares the interface to a procedure. For the syntax of procedure_spec, see "Procedures" on page 13-104.

record_declaration

Declares a user-defined record. For the syntax of record_declaration, see "Records" on page 13-110.

record_type_definition

Defines a record type using the datatype specifier RECORD or the attribute %ROWTYPE.

schema_name

The schema containing the package. If you omit schema_name, Oracle assumes the package is in your schema.

variable_declaration

Declares a variable. For the syntax of variable_declaration, see "Constants and Variables" on page 13-28.

Usage Notes

You can use any Oracle tool that supports PL/SQL to create and store packages in an Oracle database. You can issue the CREATE PACKAGE and CREATE PACKAGE BODY statements interactively from SQL*Plus, or from an Oracle Precompiler or OCI host program.

You cannot define packages in a PL/SQL block or subprogram.

Most packages have a spec and a body. The spec is the interface to your applications; it declares the types, variables, constants, exceptions, cursors, and subprograms available for use. The body fully defines cursors and subprograms, and so implements the spec.

Only subprograms and cursors have an underlying implementation. If a spec declares only types, constants, variables, exceptions, and call specs, the package body is unnecessary. The body can still be used to initialize items declared in the spec:

CREATE PACKAGE emp_actions AS

...

number_hired INTEGER; END emp_actions;

CREATE PACKAGE BODY emp_actions AS BEGIN

number_hired := 0; END emp_actions;

You can code and compile a spec without its body. Once the spec has been compiled, stored subprograms that reference the package can be compiled as well. You do not need to define the package bodies fully until you are ready to complete the application. You can debug, enhance, or replace a package body without changing the package spec, which saves you from recompiling subprograms that call the package.

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

Packages

Cursors and subprograms declared in a package spec must be defined in the package body. Other program items declared in the package spec cannot be redeclared in the package body.

To match subprogram specs and bodies, PL/SQL does a token-by-token comparison of their headers. Except for white space, the headers must match word for word. Otherwise, PL/SQL raises an exception.

Variables declared in a package keep their values throughout a session, so you can set the value of a package variable in one procedure, and retrieve the same value in a different procedure.

Related Topics

Collections, Cursors, Exceptions, Functions, Procedures, Records

PL/SQL Language Elements 13-103

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