RDB/VMS Relational Database Operator RSE — VMS RDB_4.0
A phrase that defines specific conditions that individual records must meet before Rdb/VMS includes them in a record stream. A record stream is a temporary group of related records that satisfy the conditions you specify in the record selection expression. A record stream can consist of: o All the records of one or more relations o A subset of the records in one or more relations The following sections describe how to use the record selection expression to form record streams.
Additional information available:
Format
rse =
──┬───────────────────┬───> relation-clause ──┬──────────────────┬─┐
└──> first-clause ──┘ └─> cross-clause ──┘ │
│
┌─────────────────────────────────────────────────────────────────┘
└┬──────────────────┬─┬────────────────────┬─┬──────────────────┬──>
└──> with-clause ──┘ └──> reduce-clause ──┘ └──> sort-clause ──┘
Additional information available:
first-clauserelation-clausewith-clausesort-clausereduce-clausecross-clause
first-clause
first-clause =
──────>
F
I
R
S
T ────> value-expr ──────>
Specifies how many records are in the record stream formed by the
record selection expression. If you include the element FIRST n, the
record stream has no more than n records. The argument n should be a
positive integer or a value expression that evaluates to a positive
integer. For more information on value expressions, ask for HELP on
Value_expr.
Additional information available:
More
Note the following rules:
o Rdb/VMS does not guarantee the order of records in a record
stream unless you specify a sort order. For this reason, when
you use FIRST n in the RSE, the actual records in the record
stream are unpredictable. Therefore, you should always specify
SORT when you use FIRST n.
o If n evaluates to zero or a negative number, the record stream
will be empty.
o If you specify FIRST n and n is greater than the number of
records satisfying those conditions, the record stream consists
of all records meeting the conditions of the record selection
expression.
o If n is not an integer, Rdb/VMS rounds any fractional part of the
value and uses the remaining integer as the number of records in
the record stream.
relation-clause
relation-clause =
──> context-var ───>
I
N ──┬─────────────────────┬──> relation-name ──>
└──> db-handle ──> . ─┘
In the relation-clause, you must declare context variables for a
record stream or a loop. A context variable is a temporary name that
identifies the record stream to Rdb/VMS.
Once you have associated a context variable with a relation, you use
the context variable to refer to fields from that relation. In this
way, Rdb/VMS always knows which field from which relation you are
referring to.
You must use a context variable in every data manipulation statement
and in every data definition statement that uses an RSE.
If you access several record streams at once, the context variable
lets you distinguish between fields from different record streams,
even if different fields have the same name.
In all programs except RDBPRE, RDML, SQL$PRE, and SQL$MOD programs,
context variables must be unique within a request.
with-clause
with-clause =
──────>
W
I
T
H ───> conditional-expr ───>
Allows you to specify conditions that must be true for a record to be
included in a record stream. Ask for HELP on Cond_expr for more
details.
sort-clause
sort-clause = ───>
S
O
R
T
E
D
B
Y ───┬─┬────────────────┬──> value-expr ──┬──> │ ├──>
A
S
C
E
N
D
I
N
G ──┤ │ │ └──>
D
E
S
C
E
N
D
I
N
G ─┘ │ └────────────── , <──────────────────┘ Lets you sort the records in the record stream on one or more value expressions (sort keys). The default order is ASCENDING. Rdb/VMS uses sort keys in the order you name them. Missing field values are placed last. A sort key can be any Rdb/VMS value expression. For more information on value expressions, as for HELP on Value_expr.
reduce-clause
reduce-clause = ─────>
R
E
D
U
C
E
D
T
O ───┬──> value-expr ─────┬───> └──────<──── , ──<───┘ Lets you eliminate duplicate values for fields in a record stream. You can use this expression to eliminate redundancy in the results of a query and to group the records in a relation according to a field value. Rdb/VMS does not guarantee the sorting order of the resulting record stream. To assure the desired order use the SORTED BY clause. If you use the REDUCED clause, you should retrieve only the fields that you specify in the list of value expressions. If you retrieve other fields, the results are unpredictable.
cross-clause
cross-clause = ─┬─>
C
R
O
S
S ──> relation-clause ─┬────────────────────────────┬─┬──> │ └──>
O
V
E
R ─┬─> field-name ─┬─┘ │ │ └────── , <─────┘ │ └──────────────────────────────<──────────────────────────────┘ Lets you combine records from two or more record streams. You can join these records in combinations based on the relationship between the values of fields in the separate record streams. This combining of records is called a relational join.
Examples
Choose the clause of the RSE you want to see from the following list.
Additional information available:
FIRST
Example 1
Print the first ten records in a relation:
RDO> FOR FIRST 10 E IN EMPLOYEES
cont> SORTED BY E.LAST_NAME
cont> PRINT E.*
cont> END_FOR
Example 2
Use FIRST and SORTED BY to find the maximum values for a field:
RDO> FOR FIRST 5 C IN CURRENT-SALARY
cont> SORTED BY C.SALARY_AMOUNT
cont> PRINT
cont> C.FIRST-NAME,
cont> C.LAST-NAME
cont> END_FOR
Example 3
Use FIRST with a value expression:
&RDB& GET TENTH = ( COUNT OF E IN EMPLOYEES / 10 )
&RDB& START_TRANSACTION READ_WRITE
&RDB& FOR FIRST TENTH E IN EMPLOYEES
SORTED BY E. LAST_NAME
.
.
.
relation-clause
Example 1
Use ERASE:
RDO> FOR E IN EMPLOYEES WITH E.EMPLOYEE_ID = "00334"
cont> ERASE E
cont> END_FOR
Example 2
Use MODIFY:
&RDB& FOR SH IN SALARY_HISTORY
&RDB& WITH SH.EMPLOYEE_ID = ID-NUMBER
&RDB& AND SH.SALARY_END MISSING
&RDB& MODIFY SH USING
&RDB& SH.SALARY_AMOUNT =
&RDB& ( SH.SALARY_AMOUNT + ( 1000 * RATING ) )
&RDB& END_MODIFY
&RDB& END_FOR
Example 3
Use STORE:
&RDB& STORE D IN DEPARTMENTS USING
&RDB& D.DEPARTMENT_NAME = "Recreation";
&RDB& D.DEPARTMENT_CODE = "RECR";
&RDB& D.MANAGER_ID = "00445"
&RDB& END_STORE
Example 4
Use two record streams:
&RDB& FOR E IN EMPLOYEES CROSS
&RDB& J IN JOB_HISTORY WITH
&RDB& E.EMPLOYEE_ID = J.EMPLOYEE_ID
SORT
RDO> FOR E IN EMPLOYEES SORTED BY E.BIRTHDAY
Because this example did not specify the sort order, Rdb/VMS
automatically sorts the EMPLOYEES records in ASCENDING order by
BIRTHDAY.
FOR E IN EMPLOYEES
SORTED BY DESCENDING E.STATUS_CODE,
ASCENDING E.LAST_NAME, E.SOCIAL_SECURITY
If you do not specify ASCENDING or DESCENDING for the second or
subsequent sort keys, Rdb/VMS uses the order you specified for the
preceding sort key.
REDUCED
FOR J IN JOB_HISTORY
WITH J.JOB_END MISSING
REDUCED TO J.JOB_CODE
SORTED BY J.JOB_CODE
PRINT J.JOB_CODE
END_FOR
This example lists all the currently active job codes. It includes
each value in the record stream only once.
CROSS
Create a list of employees and their current salary.
FOR E IN EMPLOYEES CROSS
SH IN SALARY_HISTORY
OVER EMPLOYEE_ID WITH
SH.SALARY_END MISSING
PRINT E.LAST_NAME,
SH.SALARY_AMOUNT
END_FOR
The EMPLOYEES relation contains, among other things, the name and
employee ID of each employee. The SALARY_HISTORY relation contains a
SALARY_AMOUNT for each salary level each employee has attained and
the starting and ending date for each current salary. For the
current salary, the salary ending date is missing. These relations
share the common field, EMPLOYEE_ID. Therefore, you can use CROSS to
join them over this field.
VIEWS
This view definition incorporates an RSE that includes a CROSS
operation.
DEFINE VIEW CURRENT_JOB OF JH IN JOB_HISTORY
CROSS E IN EMPLOYEES OVER EMPLOYEE_ID
WITH JH.JOB_END MISSING.
E.LAST_NAME.
E.FIRST_NAME.
E.EMPLOYEE_ID.
JH.JOB_CODE.
JH.DEPARTMENT_CODE.
JH.SUPERVISOR_ID.
JH.JOB_START.
JH.JOB_END.
END VIEW.