|
DECLARE CURSOR (Short Name: DEC CUR)
|
|
|
To do this...
|
Use the cursor name like this...
|
|
Set a variable to a column value
|
FETCH cursor INTO varlist SET VARIABLE varname = colname WHERE CURRENT OF cursor
|
|
Change a column value to a constant
|
UPDATE tblname SET colname + = value WHERE CURRENT OF cursor
|
|
Change a column value to a variable value
|
UPDATE tblname SET colname + = .varname WHERE CURRENT OF cursor
|
|
Change a column value to an expression
|
UPDATE tblname SET colname + = (expression) WHERE CURRENT OF cursor
|
|
Delete the pointed-to row
|
DELETE FROM tblname + WHERE CURRENT OF cursor
|
| · | Use an error variable defined with the SET ERROR VARIABLE command.
|
| · | Use the sqlcode variable.
|
| · | Include a WHENEVER NOT FOUND command.
|
|
Command Name
|
Description
|
|
CLOSE
|
Closes the open cursor but does not remove the cursor definition. However using CLOSE frees most of the memory used when a cursor is opened. CLOSE also frees any file handles used by DECLARE CURSOR.
|
|
COMMIT
|
Closes any open cursors
|
|
CONNECT
|
Removes any cursor definitions from memory
|
|
DISCONNECT
|
Removes any cursor definitions from memory
|
|
DROP CURSOR
|
Entirely removes the cursor definition. Dropping a cursor definition frees all memory used by the definition.
|
|
ROLLBACK
|
Closes any open cursors
|
|
Type of Error
|
sqlcode
|
|
Data found
|
0
|
|
Data not found
|
100
|
| 1. | DECLARE CURSOR defines the cursor path.
|
| 2. | OPEN opens the cursor, evaluates variables, and positions the cursor before the first row.
|
| 3. | The first FETCH command retrieves the first set of values. The indicator variables vi1 and vi2 capture the status values, -1 for null and 0 for a value. If you omit indicator variables in FETCH commands, R:BASE displays a message if it encounters a null value, but continues processing rows.
|
| 4. | The WHILE loop processes the rows until there are no more rows. At that point, sqlcode is set to 100, and the WHILE loop ends. Control passes to the command after ENDWHILE. If the first FETCH retrieved no data, the WHILE loop is not entered.
|
| 5. | FETCH retrieves all succeeding rows and sets sqlcodeeach time. When it does not find any more data, sqlcode is set to 100 and the WHILE loop ends.
|
| 6. | DROP CURSOR removes the cursor definition from memory.
|
|
|
| 1. | WHENEVER NOT FOUND tells R:BASE to execute GOTO if a command that searches for data, such as FETCH, cannot find more rows. If the first FETCH command does not find any rows, control passes to the command following LABEL skiploop. WHENEVER automatically checks any command that searches for data. If a data-not-found condition occurs, control passes to the command following the specified label.
|
| 2. | DECLARE CURSOR defines the cursor path.
|
| 3. | OPEN opens the cursor, evaluates the variables, and positions the cursor before the first row.
|
| 4. | The first FETCH command retrieves the first set of values. If no rows match, control passes to LABEL skiploop. Indicator variables vi1 and vi2 capture the status values (-1 for null and 0 for a value). If you omit indicator variables in FETCH commands, R:BASE displays a message if it encounters a null value, but continues processing rows. (WHENEVER instructs R:BASE to exit the WHILE loop only when sqlcode is 100.)
|
| 5. | The WHILE loop processes rows until WHENEVER stops execution.
|
| 6. | This label defines where to pass control if a data-not-found condition occurs before the WHILE loop begins executing. WHENEVER includes this label name.
|
| 7. | DROP CURSOR removes the cursor definition from memory.
|