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

GOTO Statement

GOTO Statement

The GOTO statement branches unconditionally to a statement label or block label. The label must be unique within its scope and must precede an executable statement or a PL/SQL block. The GOTO statement transfers control to the labelled statement or block. For more information, see "Using the GOTO Statement" on page 4-12.

Syntax

label_declaration

<< label_name >>

goto_statement

GOTO label_name ;

Keyword and Parameter Description

label_name

A label that you assigned to an executable statement or a PL/SQL block. A GOTO statement transfers control to the statement or block following <<label_name>>.

Usage Notes

Some possible destinations of a GOTO statement are not allowed. In particular, a GOTO statement cannot branch into an IF statement, LOOP statement, or sub-block.

From the current block, a GOTO statement can branch to another place in the block or into an enclosing block, but not into an exception handler. From an exception handler, a GOTO statement can branch into an enclosing block, but not into the current block.

If you use the GOTO statement to exit a cursor FOR loop prematurely, the cursor is closed automatically. The cursor is also closed automatically if an exception is raised inside the loop.

A given label can appear only once in a block. However, the label can appear in other blocks including enclosing blocks and sub-blocks. If a GOTO statement cannot find its target label in the current block, it branches to the first enclosing block in which the label appears.

Examples

A GOTO label cannot precede just any keyword. It must precede an executable statement or a PL/SQL block. To branch to a place that does not have an executable statement, add the NULL statement:

FOR ctr IN 1..50 LOOP DELETE FROM emp WHERE ...

IF SQL%FOUND THEN GOTO end_loop;

END IF;

...

<<end_loop>>

NULL; -- an executable statement that specifies inaction END LOOP;

PL/SQL Language Elements 13-71

IF Statement

IF Statement

The IF statement executes or skips a sequence of statements, depending on the value of a Boolean expression. For more information, see "Testing Conditions: IF and CASE Statements" on page 4-2.

Syntax

if_statement

 

 

 

 

IF

boolean_expression

THEN

statement

 

 

ELSIF

boolean_expression

THEN

statement

ELSE statement

END IF ;

Keyword and Parameter Description

boolean_expression

An expression that returns the Boolean value TRUE, FALSE, or NULL. Examples are comparisons for equality, greater-than, or less-than. The sequence following the THEN keyword is executed only if the expression returns TRUE.

ELSE

If control reaches this keyword, the sequence of statements that follows it is executed. This occurs when none of the previous conditional tests returned TRUE.

ELSIF

Introduces a Boolean expression that is evaluated if none of the preceding conditions returned TRUE.

THEN

If the expression returns TRUE, the statements after the THEN keyword are executed.

Usage Notes

There are three forms of IF statements: IF-THEN, IF-THEN-ELSE, and IF-THEN-ELSIF. The simplest form of IF statement associates a Boolean expression with a sequence of statements enclosed by the keywords THEN and END IF. The sequence of statements is executed only if the expression returns TRUE. If the expression returns FALSE or NULL, the IF statement does nothing. In either case, control passes to the next statement.

The second form of IF statement adds the keyword ELSE followed by an alternative sequence of statements. The sequence of statements in the ELSE clause is executed only if the Boolean expression returns FALSE or NULL. Thus, the ELSE clause ensures that a sequence of statements is executed.

The third form of IF statement uses the keyword ELSIF to introduce additional Boolean expressions. If the first expression returns FALSE or NULL, the ELSIF clause

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

IF Statement

evaluates another expression. An IF statement can have any number of ELSIF clauses; the final ELSE clause is optional. Boolean expressions are evaluated one by one from top to bottom. If any expression returns TRUE, its associated sequence of statements is executed and control passes to the next statement. If all expressions return FALSE or NULL, the sequence in the ELSE clause is executed.

An IF statement never executes more than one sequence of statements because processing is complete after any sequence of statements is executed. However, the THEN and ELSE clauses can include more IF statements. That is, IF statements can be nested.

Examples

In the example below, if shoe_count has a value of 10, both the first and second Boolean expressions return TRUE. Nevertheless, order_quantity is assigned the proper value of 50 because processing of an IF statement stops after an expression returns TRUE and its associated sequence of statements is executed. The expression associated with ELSIF is never evaluated and control passes to the INSERT statement.

IF shoe_count < 20 THEN order_quantity := 50;

ELSIF shoe_count < 30 THEN order_quantity := 20;

ELSE

order_quantity := 10; END IF;

INSERT INTO purchase_order VALUES (shoe_type, order_quantity);

In the following example, depending on the value of score, one of two status messages is inserted into the grades table:

IF score < 70 THEN fail := fail + 1;

INSERT INTO grades VALUES (student_id, 'Failed'); ELSE

pass := pass + 1;

INSERT INTO grades VALUES (student_id, 'Passed'); END IF;

Related Topics

CASE Statement, Expressions

PL/SQL Language Elements 13-73

INSERT Statement

INSERT Statement

The INSERT statement adds one or more new rows of data to a database table. For a full description of the INSERT statement, see Oracle Database SQL Reference.

Syntax

insert_statement

 

 

 

 

 

 

table_reference

 

alias

 

 

 

 

 

INSERT INTO

(

subquery

)

 

 

TABLE

(

subquery2

)

 

,

 

 

 

 

(

column_name

)

 

 

 

 

 

,

 

returning_clause

 

 

 

 

 

VALUES

(

sql_expression

)

 

;

subquery3

Keyword and Parameter Description

alias

Another (usually short) name for the referenced table or view.

column_name[, column_name]...

A list of columns in a database table or view. The columns can be listed in any order, as long as the expressions in the VALUES clause are listed in the same order. Each column name can only be listed once. If the list does not include all the columns in a table, each missing columns is set to NULL or to a default value specified in the CREATE TABLE statement.

returning_clause

Returns values from inserted rows, eliminating the need to SELECT the rows afterward. You can retrieve the column values into variables or into collections. You cannot use the RETURNING clause for remote or parallel inserts. If the statement does not affect any rows, the values of the variables specified in the RETURNING clause are undefined. For the syntax of returning_clause, see "DELETE Statement" on

page 13-41.

sql_expression

Any expression valid in SQL. For example, it could be a literal, a PL/SQL variable, or a SQL query that returns a single value. For more information, see Oracle Database SQL Reference. PL/SQL also lets you use a record variable here.

subquery

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

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

INSERT Statement

subquery3

A SELECT statement that returns a set of rows. Each row returned by the select statement is inserted into the table. The subquery must return a value for every column in the column list, or for every column in the table if there is no column list.

table_reference

A table or view that must be accessible when you execute the INSERT statement, and for which you must have INSERT privileges. For the syntax of table_reference, see "DELETE Statement" on page 13-41.

TABLE (subquery2)

The operand of TABLE is a SELECT statement that returns a single column value representing a nested table. This operator specifies that the value is a collection, not a scalar value.

VALUES (...)

Assigns the values of expressions to corresponding columns in the column list. If there is no column list, the first value is inserted into the first column defined by the CREATE TABLE statement, the second value is inserted into the second column, and so on.

There must be one value for each column in the column list. The datatypes of the values being inserted must be compatible with the datatypes of corresponding columns in the column list.

Usage Notes

Character and date literals in the VALUES list must be enclosed by single quotes ('). Numeric literals are not enclosed by quotes.

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

Examples

The following examples show various forms of INSERT statement:

INSERT INTO bonus SELECT ename, job, sal, comm FROM emp WHERE comm > sal * 0.25;

...

INSERT INTO emp (empno, ename, job, sal, comm, deptno)

VALUES (4160, 'STURDEVIN', 'SECURITY GUARD', 2045, NULL, 30);

...

INSERT INTO dept

VALUES (my_deptno, UPPER(my_dname), 'CHICAGO');

Related Topics

DELETE Statement, SELECT INTO Statement, UPDATE Statement

PL/SQL Language Elements 13-75

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