The FETCH statement positions a cursor on a row of the result table. It can return zero, one, or multiple rows, and it assigns the values of the rows returned to variables.
This statement can only be embedded in an application program. It is an executable statement that cannot be dynamically prepared. Multiple row fetch is not allowed in a REXX procedure.
See DECLARE CURSOR for an explanation of the authorization required to use a cursor.
.-FROM-. >>-FETCH--+------------------------+--+------+--cursor-name-----> +-NEXT-------------------+ +-PRIOR------------------+ +-FIRST------------------+ +-LAST-------------------+ | (1) | +-BEFORE-----------------+ | (2) | +-AFTER------------------+ +-CURRENT----------------+ '-RELATIVE--+-variable-+-' '-integer--' >--+--------------------+-------------------------------------->< +-single-fetch-------+ '-multiple-row-fetch-' single-fetch: .-,--------. V | |--+-INTO----variable-+-------------------------------------+---| | .-LOCAL--. | +-INTO--SQL--DESCRIPTOR--+--------+--SQL-descriptor-name-+ | '-GLOBAL-' | '-INTO DESCRIPTOR--descriptor-name-----------------------' multiple-row-fetch: |--FOR--+-variable-+--ROWS--------------------------------------> '-integer--' >--+-INTO--host-structure-array----------------------------------------------------------+--| | .-LOCAL--. | '-+-USING--SQL--DESCRIPTOR--+--------+--SQL-descriptor-name-+--INTO--row-storage-area-' | '-GLOBAL-' | '-USING DESCRIPTOR--descriptor-name-----------------------' row-storage-area: |--:--host-identifier-1--+-------------------------------------+--| | .-INDICATOR-. | '-+-----------+--:--host-identifier-2-'
The following keywords specify a new position for the cursor: NEXT, PRIOR, FIRST, LAST, BEFORE, AFTER, CURRENT, and RELATIVE. Of those keywords, only NEXT may be used for cursors that have not been declared SCROLL.
Specification | Alternative |
---|---|
RELATIVE +1 | NEXT |
RELATIVE -1 | PRIOR |
RELATIVE 0 | CURRENT |
If a single-fetch or multiple-row-fetch clause is not specified, no data is returned to the user. However, the cursor is positioned and a row lock may be acquired. For more information about locking, see Isolation level.
Before the FETCH statement is processed, the user must set the following fields in the SQLDA. (The rules for REXX are different. For more information see the Embedded SQL Programming book.)
The SQLDA must have enough storage to contain all SQLVAR occurrences. Therefore, the value in SQLDABC must be greater than or equal to 16 + SQLN*(80), where 80 is the length of an SQLVAR occurrence. If LOBs are specified, there must be two SQLVAR entries for each parameter marker and SQLN must be set to two times the number of parameter markers.
SQLD must be set to a value greater than or equal to zero and less than or equal to SQLN. For more information, see Appendix D. SQLDA (SQL descriptor area).
For example, FETCH PRIOR FROM C1 FOR 3 ROWS causes the previous row, the current row, and the next row to be returned, in that order. The cursor is positioned on the next row. FETCH RELATIVE -1 FROM C1 FOR 3 ROWS returns the same result. FETCH FIRST FROM C1 FOR :x ROWS returns the first x rows, and leaves the cursor positioned on row number x.
When a multiple-row-fetch is successfully executed, three statement information items are available in the SQL Diagnostics Area (or the SQLCA):
The first structure in the array corresponds to the first row, the second structure in the array corresponds to the second row, and so on. In addition, the first value in the row corresponds to the first item in the structure, the second value in the row corresponds to the second item in the structure, and so on. The number of rows to be fetched must be less than or equal to the dimension of the host structure array.
The COUNT field in the descriptor header must be set to reflect the number of columns in the result set. The TYPE and DATETIME_INTERVAL_CODE (if applicable) must be set for each column in the result set.
Before the FETCH statement is processed, the user must set the following fields in the SQLDA:
The values of the other fields of the SQLDA (such as SQLNAME) may not be defined after the FETCH statement is executed and should not be used.
The SQLDA must have enough storage to contain all SQLVAR occurrences. Therefore, the value in SQLDABC must be greater than or equal to 16 + SQLN*(80), where 80 is the length of an SQLVAR occurrence. If LOBs or distinct types are specified, there must be two SQLVAR entries for each parameter marker and SQLN must be set to two times the number of parameter markers.
SQLD must be set to a value greater than or equal to zero and less than or equal to SQLN. For more information, see Appendix D. SQLDA (SQL descriptor area).
On completion of the FETCH, the SQLDATA pointer in the first SQLVAR entry addresses the returned value for the first column in the allocated storage in the first row, the SQLDATA pointer in the second SQLVAR entry addresses the returned value for the second column in the allocated storage in the first row, and so on. The SQLIND pointer in the first nullable SQLVAR entry addresses the first indicator value, the SQLIND pointer in the second nullable SQLVAR entry addresses the second indicator value, and so on. The SQLDA must be allocated on a 16-byte boundary.
host-identifier-2 identifies the optional indicator area. It should be specified if any of the data types returned are nullable. The indicators are returned as small integers. host-identifier-2 must be large enough to contain an indicator for each nullable value for each row to be returned.
The GET DIAGNOSTICS statement can be used to return the DB2_ROW_LENGTH which indicates the length of each row returned into the row-storage-area.
The nth variable identified by the INTO clause or described in the SQLDA corresponds to the nth column of the result table of the cursor. The data type of each variable must be compatible with its corresponding column.
Each assignment to a variable is made according to the retrieval assignment rules described in Retrieval assignment. If the number of variables is less than the number of values in the row, the SQLSTATE is set to '01503' (or the SQLWARN3 field of the SQLCA is set to 'W'). Note that there is no warning if there are more variables than the number of result columns. If the value is null, an indicator variable must be provided. If an assignment error occurs, the value is not assigned to the variable, and no more values are assigned to variables. Any values that have already been assigned to variables remain assigned.
If an error occurs as the result of an arithmetic expression in the SELECT list of an outer SELECT statement (division by zero, overflow, etc.) or a character conversion error occurs, the result is the null value. As in any other case of a null value, an indicator variable must be provided. The value of the variable is undefined. In this case, however, the indicator variable is set to -2. Processing of the statement continues as if the error had not occurred. (However, a warning is returned.) If you do not provide an indicator variable, an error is returned. It is possible that some values have already been assigned to variables and will remain assigned when the error occurs.
multiple-row-fetch is not allowed if any of the result columns are LOBs or if the current connection is to a remote server.
Cursor position: An open cursor has three possible positions:
If a cursor is positioned on a row, that row is called the current row of the cursor. A cursor referenced in an UPDATE or DELETE statement must be positioned on a row. A cursor can only be positioned on a row as a result of a FETCH statement.
It is possible for an error to occur that makes the state of the cursor unpredictable.
Variable assignment: If the specified variable is character and is not large enough to contain the result, a warning (SQLSTATE 01004) is returned (and 'W' is assigned to SQLWARN1 in the SQLCA). The actual length of the result is returned in the indicator variable associated with the variable, if an indicator variable is provided.
If the specified variable is a C NUL-terminated variable and is not large enough to contain the result and the NUL-terminator:
Syntax alternatives: The following keywords are synonyms supported for compatibility to prior releases. These keywords are non-standard and should not be used:
Example 1: In this C example, the FETCH statement fetches the results of the SELECT statement into the program variables dnum, dname, and mnum. When no more rows remain to be fetched, the not found condition is returned.
EXEC SQL DECLARE C1 CURSOR FOR SELECT DEPTNO, DEPTNAME, MGRNO FROM TDEPT WHERE ADMRDEPT = 'A00'; EXEC SQL OPEN C1; while (SQLCODE==0) { EXEC SQL FETCH C1 INTO :dnum, :dname, :mnum; } EXEC SQL CLOSE C1;
Example 2: This FETCH statement uses an SQLDA.
FETCH CURS USING DESCRIPTOR :sqlda3