FOR statement

Description

Repeats the execution of a statement list once for each row in a cursor.

Syntax

statement-label: ]
FOR for-loop-name AS cursor-namecursor-type ] CURSOR
   { FOR statement
... [ { FOR { UPDATE cursor-concurrency | FOR READ ONLY } ]
    | USING variable-name }
    DO statement-list
END FORstatement-label ]

Parameters

cursor-type:

NO SCROLL  | DYNAMIC SCROLL  | SCROLL  | INSENSITIVE  | SENSITIVE

cursor-concurrency:

BY { VALUES | TIMESTAMP | LOCK ]

variable-name:

identifier

Examples

Example 1

This code fragment illustrates the use of the FOR loop:

FOR names AS curs CURSOR FOR
SELECT Surname
FROM Employees
DO
	CALL search_for_name( Surname );
END FOR;

Usage

FOR is a control statement that lets you execute a list of SQL statements once for each row in a cursor. The FOR statement is equivalent to a compound statement with a DECLARE for the cursor and a DECLARE of a variable for each column in the result set of the cursor followed by a loop that fetches one row from the cursor into the local variables and executes statement-list once for each row in the cursor.

For descriptions of the cursor-type parameters and more examples, see “FOR statement” in SQL Anywhere Server – SQL Reference > Using SQL > SQL statements > SQL statements (E-O).

The name and data type of the local variables that are declared are derived from the statement used in the cursor. With a SELECT statement, the data type is the data type of the expressions in the select list. The names are the select list item aliases where they exist; otherwise, they are the names of the columns. Any select list item that is not a simple column reference must have an alias. With a CALL statement, the names and data types are taken from the RESULT clause in the procedure definition.

The LEAVE statement can be used to resume execution at the first statement after the END FOR. If the ending statement-label is specified, it must match the beginning statement-label.


Side effects

None

Standards

Permissions

None

See also

DECLARE CURSOR statement [ESQL] [SP]

FETCH statement [ESQL] [SP]

LEAVE statement

LOOP statement