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

Comments

Comments

Comments let you include arbitrary text within your code to explain what the code does. You can also disable obsolete or unfinished pieces of code by turning them into comments.

PL/SQL supports two comment styles: single-line and multi-line. A double hyphen (--) anywhere on a line (except within a character literal) turns the rest of the line into a comment. Multi-line comments begin with a slash-asterisk (/*) and end with an asterisk-slash (*/). For more information, see "Comments" on page 2-7.

Syntax

comment

- - text

/* text */

Usage Notes

Single-line comments can appear within a statement at the end of a line.

You can include single-line comments inside multi-line comments, but you cannot nest multi-line comments.

You cannot use single-line comments in a PL/SQL block that will be processed dynamically by an Oracle Precompiler program. End-of-line characters are ignored, making the single-line comments extend to the end of the block. Instead, use multi-line comments.

While testing or debugging a program, you might want to disable a line of code. The following example shows how you can "comment-out" the line:

-- UPDATE department SET location_id = my_loc WHERE department_id = my_deptno;

You can use multi-line comment delimiters to comment-out whole sections of code.

Examples

The following examples show various comment styles:

DECLARE

area NUMBER; pi NUMBER; radius NUMBER; BEGIN

-- Compute the area of a circle

area := pi * radius**2; -- pi is approx. 3.14159

/*

Compute the area of a circle.

*/

area := pi /* pi is approx. 3.14159 */ * radius**2; END;

/

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

COMMIT Statement

COMMIT Statement

The COMMIT statement makes permanent any changes made to the database during the current transaction. A commit also makes the changes visible to other users. For more information, see "Overview of Transaction Processing in PL/SQL" on page 6-29.

Syntax

commit_statement

 

WORK

COMMENT ’ text ’

COMMIT

;

Keyword and Parameter Description

COMMENT

Specifies a comment to be associated with the current transaction. Typically used with distributed transactions. The text must be a quoted literal no more than 50 characters long.

WORK

Optional, for readability only.

Usage Notes

The COMMIT statement releases all row and table locks, and erases any savepoints you marked since the last commit or rollback. Until your changes are committed:

You can see the changes when you query the tables you modified, but other users cannot see the changes.

If you change your mind or need to correct a mistake, you can use the ROLLBACK statement to roll back (undo) the changes.

If you commit while a FOR UPDATE cursor is open, a subsequent fetch on that cursor raises an exception. The cursor remains open, so you should still close it. For more information, see "Using FOR UPDATE" on page 6-33.

When a distributed transaction fails, the text specified by COMMENT helps you diagnose the problem. If a distributed transaction is ever in doubt, Oracle stores the text in the data dictionary along with the transaction ID. For more information about distributed transactions, see Oracle Database Concepts.

In SQL, the FORCE clause manually commits an in-doubt distributed transaction. PL/SQL does not support this clause:

COMMIT WORK FORCE '23.51.54'; -- not allowed

In embedded SQL, the RELEASE option frees all ocks and cursors held by a program and disconnects from the database. PL/SQL does not support this option:

COMMIT WORK RELEASE; -- not allowed

Related Topics

ROLLBACK Statement, SAVEPOINT Statement

PL/SQL Language Elements 13-27

Constants and Variables

Constants and Variables

You can declare constants and variables in the declarative part of any PL/SQL block, subprogram, or package. Declarations allocate storage for a value, specify its datatype, and specify a name that you can reference. Declarations can also assign an initial value and impose the NOT NULL constraint. For more information, see Declarations on

page 2-8.

Syntax

variable_declaration

 

NOT NULL

:=

 

 

 

expression

 

 

 

DEFAULT

variable_name

datatype

 

;

datatype

collection_name % TYPE collection_type_name

cursor_name % ROWTYPE cursor_variable_name % TYPE

% ROWTYPE db_table_name

. column_name % TYPE

object_name % TYPE

REF

object_type_name record_name % TYPE record_type_name ref_cursor_type_name scalar_datatype_name

variable_name

%

TYPE

 

 

 

constant_declaration

 

 

NOT NULL

 

 

 

 

 

:=

 

constant_name

CONSTANT

datatype

expression

;

 

 

 

 

DEFAULT

 

Keyword and Parameter Description

collection_name

A collection (associative array, nested table, or varray) previously declared within the current scope.

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

Constants and Variables

collection_type_name

A user-defined collection type defined using the datatype specifier TABLE or VARRAY.

CONSTANT

Denotes the declaration of a constant. You must initialize a constant in its declaration. Once initialized, the value of a constant cannot be changed.

constant_name

A program constant. For naming conventions, see "Identifiers" on page 2-3.

cursor_name

An explicit cursor previously declared within the current scope.

cursor_variable_name

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

db_table_name

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

db_table_name.column_name

A database table and column 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 constant or variable. The value and the constant or variable must have compatible datatypes.

NOT NULL

A constraint that prevents the program from assigning a null value to a variable or constant. Assigning a null to a variable defined as NOT NULL raises the predefined exception VALUE_ERROR. The constraint NOT NULL must be followed by an initialization clause.

object_name

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

record_name

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

record_name.field_name

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

record_type_name

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

ref_cursor_type_name

A user-defined cursor variable type, defined using the datatype specifier REF CURSOR.

PL/SQL Language Elements 13-29

Constants and Variables

%ROWTYPE

Represents a record that can hold a row from a database table or a cursor. Fields in the record have the same names and datatypes as columns in the row.

scalar_datatype_name

A predefined scalar datatype such as BOOLEAN, NUMBER, or VARCHAR2. Includes any qualifiers for size, precision, or character versus byte semantics.

%TYPE

Represents the datatype of a previously declared collection, cursor variable, field, object, record, database column, or variable.

variable_name

A program variable.

Usage Notes

Constants and variables are initialized every time a block or subprogram is entered. By default, variables are initialized to NULL.

Whether public or private, constants and variables declared in a package spec are initialized only once for each session.

An initialization clause is required when declaring NOT NULL variables and when declaring constants. If you use %ROWTYPE to declare a variable, initialization is not allowed.

You can define constants of complex types that have no literal values or predefined constructors, by calling a function that returns a filled-in value. For example, you can make a constant associative array this way.

Examples

Several examples of variable and constant declarations follow:

credit_limit

CONSTANT NUMBER := 5000;

invalid

BOOLEAN := FALSE;

acct_id

INTEGER(4) NOT NULL DEFAULT 9999;

pi

CONSTANT REAL := 3.14159;

postal_code

VARCHAR2(20);

last_name

VARCHAR2(20 CHAR);

my_ename

emp.ename%TYPE;

Related Topics

"Declarations" on page 2-8, "Overview of Predefined PL/SQL Datatypes" on page 3-1, Assignment Statement, Expressions, %ROWTYPE Attribute, %TYPE Attribute

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

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