Note the following when coding DB-DECLARE:
Unqualified, select all columns.
DB-DECLARE cursorname copylibname-REC ... [FETCH ONLY|READ ONLY] ... [FETCH FIRST numberofrows] ... [WITH [CS|RS|RR|UR]] ... [SENSITIVE [DYNAMIC | STATIC] SCROLL CURSOR|INSENSITIVE SCROLL CURSOR] ... [WITH ROWSET POSITIONING] ... [QUERYNO number] ... [WITH HOLD] ... [OPTIMIZE number] ... [UPDATE|ORDER ... column1 [ASC|DESC] [...columnN [ASC|DESC]]]
Qualified, select all columns.
DB-DECLARE cursorname copylibname-REC ... [FETCH ONLY|READ ONLY] ... [FETCH FIRST numberofrows] ... [WITH [CS|RS|RR|UR]] ... [SENSITIVE [DYNAMIC | STATIC] SCROLL CURSOR|INSENSITIVE SCROLL CURSOR] ... [WITH ROWSET POSITIONING] ... [QUERYNO number] ... [WITH HOLD] ... [OPTIMIZE number] ... WHERE column operator [[:]altvalue]|column ... [AND|OR column operator [[:]altvalue]|column] ... [UPDATE|ORDER ... column1 [ASC|DESC] [...columnN [ASC|DESC]]]
Select specific columns.
DB-DECLARE cursorname copylibname-REC ... [DISTINCT] ... [FETCH ONLY|READ ONLY] ... [FETCH FIRST numberofrows] ... [WITH [CS|RS|RR|UR]] ... [SENSITIVE [DYNAMIC | STATIC] SCROLL CURSOR|INSENSITIVE SCROLL CURSOR] ... [WITH ROWSET POSITIONING] ... [QUERYNO number] ... [WITH HOLD] ... [OPTIMIZE number] ... column1 [(altvalue)] [... columnN [(altvalue)]] ... [WHERE column operator [[:]altvalue]|column ... [AND|OR correlname.]column operator ... [[:]altvalue]|column] . . . ... [AND|OR correlname.]column operator ... [[:]altvalue]|column]] ... [UPDATE|ORDER ... column1 [ASC|DESC] [...columnN [ASC|DESC]]]
Implicit Join selecting columns from two or more tables.
DB-DECLARE cursorname correlname.copylibname-REC ... [DISTINCT] ... [FETCH ONLY|READ ONLY] ... [FETCH FIRST numberofrows] ... [WITH [CS|RS|RR|UR]] ... [SENSITIVE [DYNAMIC | STATIC] SCROLL CURSOR|INSENSITIVE SCROLL CURSOR] ... [WITH ROWSET POSITIONING] ... [QUERYNO number] ... [WITH HOLD] ... [OPTIMIZE number] ... [column1 [(altvalue)] [... columnN [(altvalue)]]] ... [correlname.copylibname-REC ... [column1 [(altvalue)] [... columnN [(altvalue)]]] . . . ... [WHERE correlname.column oper ... [:]altvalue|correlname.column ... [AND|OR correlname.column oper ... [:]altval|correlname.column] . . . ... [AND|OR correlname.column oper ... [:]altvalue|correlname.col]] ... [ORDER ... column1 [ASC|DESC] [...columnN [ASC|DESC]]]
Specify a UNION.
DB-DECLARE cursorname copylibname-REC ... [DISTINCT] ... [FETCH ONLY|READ ONLY] ... [FETCH FIRST numberofrows] ... [WITH [CS|RS|RR|UR]] ... [SENSITIVE [DYNAMIC | STATIC] SCROLL CURSOR|INSENSITIVE SCROLL CURSOR] ... [WITH ROWSET POSITIONING] ... [QUERYNO number] ... [WITH HOLD] ... [OPTIMIZE number] ... [column1 [(altvalue)] [... columnN [(altvalue)]]] ... [WHERE column operator [[:]altvalue]|column ... [AND|OR column operator [[:]altvalue]|column] . . . ... [AND|OR column operator [[:]altvalue|]column]] ... UNION [ALL] DB-OBTAIN REC copylibname-REC . . . ... [ORDER ... column1 [ASC|DESC] [...columnN [ASC|DESC]]]
Specify a Join using a join-operator.
DB-DECLARE cursorname correlname1.copylibname-REC ... [DISTINCT] ... [FETCH ONLY|READ ONLY] ... [QUERYNO number] ... [WITH HOLD] ... [OPTIMIZE number] ... [column1 [... columnN]]|[NONE] . . . ... [[INNER JOIN|RIGHT OUTER JOIN|LEFT OUTER JOIN|FULL OUTER JOIN] ... ON joincondition REC] ... correlnameN.copylibname-REC ... [column1 [... columnN]]|[NONE] . . . ... [WHERE correlname.column1 oper ... [:]value|correlname.column2 ... [AND|OR correlname.column3 oper ... [:]value|correlname.column4 . . . ... AND|OR correlname.columnN operator ... [:]value|correlnameN]] ... [ORDER ... column1 [ASC|DESC] [...columnN [ASC]]] ... [FETCH FIRST numberofrows] ... [WITH [CS|RS|RR|UR]] ... [SENSITIVE [DYNAMIC | STATIC] SCROLL CURSOR|INSENSITIVE SCROLL CURSOR] ... [WITH ROWSET POSITIONING]
cursorname |
Cursor name (maximum 12 characters) must be unique, and cannot be the same as the subschema copylib names. |
|
copylibname-REC |
Copybook library name of source data. |
|
DISTINCT |
Eliminate all but one row from each set of duplicate rows. Duplicate rows have identical selected columns from the results table. You can use one or more DISTINCT keywords on selects. |
|
FETCH ONLY |
Specify that the table is read-only and therefore the cursor cannot be referred to in positioned UPDATE and DELETE statements. Do not code in a call that contains an UPDATE clause. |
|
READ ONLY |
Equivalent to FETCH ONLY. |
|
QUERYNO number |
Specifies the number to be used for this SQL statement in EXPLAIN output and trace records. |
|
WITH HOLD |
Prevent the closing of a cursor as a consequence of a commit operation. |
|
OPTIMIZE number |
Specify estimated maximum number of rows that call will retrieve. If the call retrieves no more than number rows, performance could be improved. Specifying this keyword does not prevent all rows from being retrieved. |
|
INNER JOIN ON joincondition |
Combines each row of the left table with every row of the right table keeping only the rows where the join-condition is true. If no join operator is specified, INNER is implicit |
|
RIGHT OUTER JOIN ON joincondition |
Includes the rows from the right table that were missing from the inner join. |
|
LEFT OUTER JOIN ON joincondition |
Includes the rows from the left table that were missing from the inner join. |
|
FULL OUTER JOIN ON joincondition |
Includes rows from both tables that were missing from the inner join. |
|
UPDATE |
Modify columns during cursor processing. In cursor processing, you cannot modify a column unless you code UPDATE first. Do not code UPDATE with UNION, DISTINCT, GROUP BY, or if call specifies a join or selects column functions. |
|
ORDER [ASC|DESC] [column1 ...columnN] |
Sort the results table in ascending (default) or descending order, based on the values in the columns specified. Specify the column either by name or by relative position in the column selection list. Specify at least one column. Do not code with UPDATE. |
|
WHERE column operator [:]altvalue |
Column is the column on which to qualify the selection. Operator can be =, ^=, >, <, >=, <=, native SQL predicates (such as LIKE and BETWEEN). See also the information on Alternate values and Host variables in the General Rules section of the Help topic Database Calls. Column names cannot exceed 18 characters. |
|
AND|OR column operator column|[:]altvalue |
Altvalue can be a literal or data name. See also the information on Alternate values and Host variables in the General Rules section of the Help topic Database Calls. |
|
UNION |
See UNION. |
|
FETCH FIRST numberofrows |
Limits the number of rows that can be fetched. |
|
WITH |
Specifies the isolation level at which the statement is executed. |
|
CS |
Cursor stability |
|
RS |
Read stability |
|
RR |
Repeatable read |
|
UR |
Uncommited read - can only be specified if the result table is read-only. |
|
SENSITIVE SCROLL CURSOR |
Cursor is scrollable. Changes made to the base table after the result table is materialized are visible to the cursor. |
|
DYNAMIC |
The result table of the cursor when it is opened is dynamic, meaning the size of the result table and the order of rows might change when rows are inserted or deleted. This is the default. |
|
STATIC |
The size of the result table and the order of the rows do not change after the cursor is opened. |
|
INSENSITIVE SCROLL CURSOR |
Cursor is scrollable. Changes made to the base table after the result table is materialized are not visible to the cursor. |
|
WITH ROWSET POSITIONING |
Retrieve multiple rows of data from the result table as a rowset into host-variable arrays. |
DB-DECLARE D2MAST-CURSOR D2TAB-REC ... UPDATE PM_COLOR PM_NEW_PART_NO
DB-DECLARE D2MAST-CURSOR D2TAB-REC ... DISTINCT ... WHERE PM_PART_SHORT_DESC = ... :WS-PART-SHORT-DESC ... AND PM_UNIT_BASE_PRICE BETWEEN 10 AND 50
DB-DECLARE D2MAST-CURSOR D2TAB-REC ... PM_PART_NO ... PM_COLOR (WS-COLOR) ... WHERE PM_PART_SHORT_DESC = ... :WS-PART-SHORT-DESC ... AND PM_UNIT_BASE_PRICE BETWEEN 10 AND 50
DB-DECLARE D2MAST-CURSOR D2TAB-REC ... DISTINCT ... PM_PART_NO PM_COLOR ... WHERE PM_PART_SHORT_DESC = ... :WS-PART-SHORT-DESC ... ORDER 2, 1
Declare a cursor set to include three columns drawn from two separate select statements, UNIONed together. Select two columns from D2TAB-REC and one column from D2INVEN-REC in the first UNION. Base selection criteria on PM_PART_NO matching IN_PART_NO, with PM_COLOR equal to the Working-Storage field WS-COLOR and IN-COLOR equal to 'BLUE'.
DB-DECLARE JOIN-CUR A.D2TAB-REC ... DISTINCT ... PM_PART_NO PM_COLOR ... B.D2INVEN_REC IN_QTY_ONHAND ... WHERE A.PM_PART_NO = B.IN_PART_NO ... AND A.PM_COLOR = :WS-COLOR ... AND B.IN_COLOR = 'BLUE' ... UNION DB-OBTAIN REC D2TAB-REC ... PM_PART_NO PM_COLOR PM_UNITS ... WHERE PM_COLOR ^= 'BLUE' ... AND PM_UNITS < 50 ... ORDER 1 ASC, 2 ASC
DB-DECLARE ORDERSCROLL ... ACCTMAST-REC ... SENSITIVE DYNAMIC SCROLL CURSOR ... WHERE ACCOUNT_NUMBER > 1000 ... UPDATE CUSTOMER_NUMBER DB-OPEN CURSOR ORDERSCROLL DB-FETCH CURSOR ORDERSCROLL ... SENSITIVE RELATIVE +3 DB-CLOSE CURSOR ORDERSCROLL
DB-DECLARE C1 ... A.ACCTMAST-REC ... WITH ROWSET POSITIONING DB-OPEN CURSOR C1 DB-FETCH CURSOR C1 ... NEXT ROWSET ... FOR 9 ROWS ... INTO ACCTMAST-REC DB-CLOSE CURSOR C1