DB2 - Banco de dados - FETCH - www.cadcobol.com.br
The FETCH statement positions a cursor on the next row of its result table and assigns the values of that row to target variables.
Although an interactive SQL facility might provide an interface that gives the appearance of interactive execution, this statement can only be embedded within an application program. It is an executable statement that cannot be dynamically prepared. When invoked using the command line processor, the syntax following cursor-name is optional and different from the SQL syntax.
For each global variable used as a cursor-variable-name or in the expression for an array-index, the privileges held by the authorization ID of the statement must include one of the following:
For each global variable used as an assignment-target, the privileges held by the authorization ID of the statement must include one of the following:
For the authorization required to use a cursor, see DECLARE CURSOR.
DECLARE CURSOR
Identifies one or more targets for the assignment of output values. The first value in the result row is assigned to the first target in the list, the second value to the second target, and so on. Each assignment to an assignment-target is made in sequence through the list. If an error occurs on any assignment, the value is not assigned to the target, and no more values are assigned to targets. Any values that have already been assigned to targets remain assigned.
When the data type of every assignment-target is not a row type, then the value 'W' is assigned to the SQLWARN3 field of the SQLCA if the number of assignment-targets is less than the number of result column values.
If the data type of an assignment-target is a row type, then there must be exactly one assignment-target specified (SQLSTATE 428HR), the number of columns must match the number of fields in the row type, and the data types of the columns of the fetched row must be assignable to the corresponding fields of the row type (SQLSTATE 42821).
If the data type of an assignment-target is an array element, then there must be exactly one assignment-target specified.
Before the FETCH statement is processed, the user must set the following fields in the SQLDA:
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*(N), where N is the length of an SQLVAR occurrence.
If LOB or structured type result columns need to be accommodated, there must be two SQLVAR entries for every select-list item (or column of the result table).
SQLD must be set to a value greater than or equal to zero and less than or equal to SQLN.
The nth variable 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 specific rules. If the number of variables is less than the number of values in the row, the SQLWARN3 field of the SQLDA is set to 'W'. Note that there is no warning if there are more variables than the number of result columns. 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 the cursor is currently positioned before a row, it will be repositioned on that row, and values will be assigned to targets as specified by the INTO or USING clause.
If the cursor is currently positioned on a row other than the last row, it will be repositioned on the next row and values of that row will be assigned to targets as specified by the INTO or USING clause.
If a cursor is 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.
It is possible for an error to occur that makes the state of the cursor unpredictable.
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;
FETCH CURS USING DESCRIPTOR :sqlda3