RDB/VMS Relational Database Operator FOR — VMS RDB_4.1A
Executes a statement or group of statements once for each record in a
record stream formed by a record selection expression (RSE). Rdb/VMS
evaluates all variables when the RSE is compiled, not when the
statements within the FOR loop execute. For detailed information on
the RSE, request HELP on RSE.
Example:
RDO> FOR E IN EMPLOYEES WITH E.EMPLOYEE_ID = "00164"
cont> MODIFY E USING E.MIDDLE_INITIAL = "M"
cont> END_MODIFY
cont> END_FOR
Additional information available:
FormatMoreExamplessegmented string FOR
Format
FOR ─┬──────────>────────┬──> rse ──┬───────>───────┬──┐ └─> handle-options ─┘ └──> on-error ──┘ │ ┌───────────────────────<──────────────────────────────┘ └─┬─> statement ─┬──────>
E
N
D
F
O
R ─────────> └────────<─────┘
Additional information available:
rseon-errorstatementhandle-options
rse
Any valid record selection expression. Request HELP on RSE for a complete discussion of record selection expressions.
on-error
ON ERROR ─┬─> statement ─┬─>
E
N
D
E
R
R
O
R └────────<─────┘ The ON ERROR clause. This clause specifies the action to be taken if an error occurs while Rdb/VMS is compiling the RSE. Request HELP for ON_ERROR for more information.
statement
Any valid Rdb/VMS data manipulation statement or host language statement except INVOKE, COMMIT, or ROLLBACK. No statement within the FOR loop can redefine the context variable that was defined by the RSE in the FOR statement.
handle-options
handle-options = ──> ( ─┬─>
R
E
Q
U
E
S
T
H
A
N
D
L
E ───> var ───────────────────────────┬─> ) ──> ├─>
T
R
A
N
S
A
C
T
I
O
N
H
A
N
D
L
E ───> var ───────────────────────┤ └─>
R
E
Q
U
E
S
T
H
A
N
D
L
E ─> var , TRANSACTIONHANDLE ─> var ─┘
Additional information available:
REQUEST_HANDLETRANSACTION_HANDLE
REQUEST_HANDLE
A keyword followed by a host language variable. A request handle points to the location of a compiled Rdb/VMS request. If you do not supply a request handle explicitly, Rdb/VMS associates a default request handle with the compiled request. Your must use a request handle when you want to make an identical query to two different databases. In Callable RDO, use !VAL as a marker for host language variables. You can put parentheses around the host language variable name.
TRANSACTION_HANDLE
A keyword followed by a host language variable. A transaction handle identifies each instance of a transaction. If you do not declare the transaction handle explicitly, Rdb/VMS attaches an internal identifier to the transaction. In Callable RDO, use !VAL as a marker for host language variables. You can put parentheses around the host language variable name. Normally, you do not need to use this argument. The ability to declare a transaction handle is provided for compatibility with other database products and future releases of Rdb/VMS.
More
You need the Rdb/VMS READ privilege to the records in a record stream to use the FOR statement. You can nest FOR loops as an alternative to the CROSS clause to perform a join operation. However, the performance of the CROSS clause is usually faster.
Examples
Example 1
Create a record stream with a FOR statement in RDO:
RDO> START_TRANSACTION READ_ONLY
RDO>
RDO> FOR D IN DEPARTMENTS WITH D.DEPARTMENT_CODE = "SEUR"
cont> PRINT D.DEPARTMENT_CODE,
cont> D.DEPARTMENT_NAME,
cont> D.MANAGER_ID
cont> END_FOR
RDO>
RDO> COMMIT
These statements:
o Create a record stream defined by a record selection expression
o Retrieve three field values from each record in that stream
Example 2
Create a record stream with the FOR statement in BASIC:
&RDB& START_TRANSACTION READ_ONLY
&RDB& FOR E IN EMPLOYEES CROSS
&RDB& S IN SALARY_HISTORY OVER EMPLOYEE_ID
&RDB& WITH E.EMPLOYEE_ID = EMPLOYEE_ID
&RDB& AND S.SALARY_END MISSING
&RDB& ON ERROR
GOTO 3000
&RDB& END_ERROR
&RDB& GET
&RDB& LAST_NAME = E.LAST_NAME;
&RDB& FIRST_NAME = E.FIRST_NAME;
&RDB& SALARY = S.SALARY_AMOUNT
&RDB& END_GET
&RDB& END_FOR
&RDB& COMMIT
This program fragment retrieves the current salary for an employee
specified by the value of the EMPLOYEE_ID variable. The example:
o Establishes a record stream consisting of the record in the
EMPLOYEES relation with the ID number that the user supplies in
the host language variable EMPLOYEE_ID, joined with the
corresponding current SALARY_HISTORY record
o Points to an error-handling subroutine, in case of errors from
Rdb/VMS
o Assigns the values from the FIRST_NAME and LAST_NAME fields of
EMPLOYEES and the SALARY_AMOUNT field of SALARY_HISTORY to host
variables
segmented string FOR
A special form of the FOR statement sets up a record stream
consisting of segments from a segmented string field. Because a
single segmented string field value is made up of multiple segments,
a record stream that includes a segmented string field is "nested."
The outer loop retrieves records that include the field and the inner
loop retrieves the segments of each field value one at a time.
Therefore, a FOR statement that retrieves segmented strings looks
like a set of nested FOR statements.
Example:
RDO> FOR R IN RESUMES
cont> FOR S IN R.RESUME
cont> PRINT S.RDB$LENGTH, S.RDB$VALUE
cont> END_FOR
cont> END_FOR
Additional information available:
Format
FOR ──> context-var ───> typebox (I)typebox (N) ──> ss-field ──┬───────>──────┬─┐ └─> on-error ──┘ │ ┌────────────────────────<────────────────────────────┘ └─┬─> get-statement ─┬───>
E
N
D
F
O
R ───> └────────<─────────┘
Additional information available:
context-varss-fieldon-errorget-statement
context-var
A context variable.
ss-field
A qualified field name that refers to a field defined with the SEGMENTED STRING data type. Note that this field name, like all field names in a FOR statement, must be qualified by its own context variable. This second context variable must match the variable declared in the outer FOR statement. See the Examples topic.
on-error
The ON ERROR clause. This clause specifies the action to be taken if an Rdb/VMS error occurs while Rdb/VMS is trying to set up retrieval of the next segmented string. For more details, request HELP on ON_ERROR.
get-statement
Any valid Rdb/VMS data manipulation language or host language statement except INVOKE, COMMIT, or ROLLBACK. The GET statement can reference only the RDB$VALUE and RDB$LENGTH fields.
More
If you have invoked a database, you have the necessary privileges to
use the FOR Statement with Segmented Strings.
Rdb/VMS defines a special name to refer to the segments of a
segmented string. This value expression is equivalent to a field
name; it names the "fields" or segments of the string. Furthermore,
because segments can vary in length, Rdb/VMS also defines a name for
the length of a segment. The statement inside the segmented string
FOR loop must use these names to refer to the segments of the string.
These names are:
RDB$VALUE The value stored in a segment of a segmented
string
RDB$LENGTH The length in bytes of a segment
Example
Create a stream whose records contain segmented string fields:
RDO> FOR R IN RECORD
cont> FOR S IN R.SS_FIELD
cont> PRINT S.RDB$LENGTH, S.RDB$VALUE
cont> END_FOR
cont> END_FOR
This statement looks like a nested FOR loop.
o The outer loop sets up a record stream using the context variable
R. The same context variable qualifies the field name, SS_FIELD,
as in every FOR statement.
o The inner loop retrieves the segments of the string field one at
a time.
o The context variable S identifies the segments.
o The special segmented string value expressions, RDB$VALUE and
RDB$LENGTH are qualified by S, the context variable associated
with the field.