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

13

PL/SQL Language Elements

Grammar, which knows how to control even kings. —Molière

This chapter is a quick reference guide to PL/SQL syntax and semantics. It shows you how commands, parameters, and other language elements are combined to form PL/SQL statements. It also provides usage notes and short examples.

This chapter contains these topics:

Assignment Statement

AUTONOMOUS_TRANSACTION Pragma

Blocks

CASE Statement

CLOSE Statement

Collection Methods

Collections

Comments

COMMIT Statement

Constants and Variables

Cursor Attributes

Cursor Variables

Cursors

DELETE Statement

EXCEPTION_INIT Pragma

Exceptions

EXECUTE IMMEDIATE Statement

EXIT Statement

Expressions

FETCH Statement

FORALL Statement

Functions

GOTO Statement

PL/SQL Language Elements 13-1

IF Statement

INSERT Statement

Literals

LOCK TABLE Statement

LOOP Statements

MERGE Statement

NULL Statement

Object Types

OPEN Statement

OPEN-FOR Statement

OPEN-FOR-USING Statement

Packages

Procedures

RAISE Statement

Records

RESTRICT_REFERENCES Pragma

RETURN Statement

ROLLBACK Statement

%ROWTYPE Attribute

SAVEPOINT Statement

SCN_TO_TIMESTAMP Function

SQLCODE Function

SELECT INTO Statement

SERIALLY_REUSABLE Pragma

SET TRANSACTION Statement

SQL Cursor

SQLCODE Function

SQLERRM Function

TIMESTAMP_TO_SCN Function

%TYPE Attribute

UPDATE Statement

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

Assignment Statement

Assignment Statement

An assignment statement sets the current value of a variable, field, parameter, or element. The statement consists of an assignment target followed by the assignment operator and an expression. When the statement is executed, the expression is evaluated and the resulting value is stored in the target. For more information, see "Assigning Values to Variables" on page 2-16.

Syntax

assignment_statement

( index )

collection_name cursor_variable_name

: host_cursor_variable_name

: indicator_name

: host_variable_name

.

attribute_name

:=

expression

;

 

 

 

object_name parameter_name

. field_name

record_name variable_name

Keyword and Parameter Description

attribute_name

An attribute of an object type. The name must be unique within the object type (but can be reused in other object types). You cannot initialize an attribute in its declaration using the assignment operator or DEFAULT clause. Also, you cannot impose the NOT NULL constraint on an attribute.

collection_name

A nested table, index-by table, or varray previously declared within the current scope.

cursor_variable_name

A PL/SQL cursor variable previously declared within the current scope. Only the value of another cursor variable can be assigned to a cursor variable.

expression

A combination of variables, constants, literals, operators, and function calls. The simplest expression consists of a single variable. For the syntax of expression, see "Expressions" on page 13-52. When the assignment statement is executed, the expression is evaluated and the resulting value is stored in the assignment target. The value and target must have compatible datatypes.

PL/SQL Language Elements 13-3

Assignment Statement

field_name

A field in a user-defined or %ROWTYPE record.

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.

host_variable_name

A variable declared in a PL/SQL host environment and passed to PL/SQL as a bind variable. Host variables must be prefixed with a colon.

index

A numeric expression that must return a value of type BINARY_INTEGER or a value implicitly convertible to that datatype.

indicator_name

An indicator variable declared in a PL/SQL host environment and passed to PL/SQL. Indicator variables must be prefixed with a colon. An indicator variable "indicates" the value or condition of its associated host variable. For example, in the Oracle Precompiler environment, indicator variables let you detect nulls or truncated values in output host variables.

object_name

An instance of an object type previously declared within the current scope.

parameter_name

A formal OUT or IN OUT parameter of the subprogram in which the assignment statement appears.

record_name

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

variable_name

A PL/SQL variable previously declared within the current scope.

Usage Notes

By default, unless a variable is initialized in its declaration, it is initialized to NULL every time a block or subprogram is entered. Always assign a value to a variable before using that variable in an expression.

You cannot assign nulls to a variable defined as NOT NULL. If you try, PL/SQL raises the predefined exception VALUE_ERROR.

Only the values TRUE, FALSE, and NULL can be assigned to a Boolean variable.

You can assign the result of a comparison or other test to a Boolean variable.

You can assign the value of an expression to a specific field in a record.

You can assign values to all fields in a record at once. PL/SQL allows aggregate assignment between entire records if their declarations refer to the same cursor or table. The following example copies values from all the fields of one record to another:

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

Assignment Statement

DECLARE

emp_rec1 emp%ROWTYPE; emp_rec2 emp%ROWTYPE; dept_rec dept%ROWTYPE;

BEGIN

...

emp_rec1 := emp_rec2;

You can assign the value of an expression to a specific element in a collection, by subscripting the collection name.

Examples

DECLARE

wages NUMBER; hours_worked NUMBER; hourly_salary NUMBER; bonus NUMBER; country VARCHAR2(128);

counter NUMBER := 0; done BOOLEAN;

emp_rec employees%ROWTYPE;

TYPE commissions IS TABLE OF NUMBER INDEX BY PLS_INTEGER; comm_tab commissions;

BEGIN

wages := (hours_worked * hourly_salary) + bonus; country := 'France';

country := UPPER('Canada'); done := (counter > 100); emp_rec.first_name := 'Antonio'; comm_tab(5) := 20000 * 0.15;

END;

/

Related Topics

Constants and Variables, Expressions, SELECT INTO Statement

PL/SQL Language Elements 13-5

Тут вы можете оставить комментарий к выбранному абзацу или сообщить об ошибке.

Мы не исправляем ошибки в тексте (почему?), но будем благодарны, если вы все же напишите об ошибках.

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