7.6.14. FETCH

Used for

Fetching successive records from a data set retrieved by a cursor

Available in

PSQL

Syntax

  1. FETCH cursorname INTO [:]varname [, [:]varname ...]
Table 91. FETCH Statement Parameters
ArgumentDescription

cursorname

Cursor name. A cursor with this name must be previously declared with a DECLARE CURSOR statement and opened by an OPEN statement.

varname

Variable name

A FETCH statement fetches the first and successive rows from the result set of the cursor and assigns the column values to PSQL variables. The FETCH statement can be used only with a cursor declared with the DECLARE CURSOR statement.

The INTO clause gets data from the current row of the cursor and loads them into PSQL variables.

For checking whether all of the the data set rows have been fetched, the context variable ROW_COUNT returns the number of rows fetched by the statement. It is positive until all rows have been checked. A ROW_COUNT of 1 indicates that the next fetch will be the last.

Example

Using the FETCH statement:

  1. SET TERM ^;
  2. CREATE OR ALTER PROCEDURE GET_RELATIONS_NAMES
  3. RETURNS (
  4. RNAME CHAR(31)
  5. )
  6. AS
  7. DECLARE C CURSOR FOR (
  8. SELECT RDB$RELATION_NAME
  9. FROM RDB$RELATIONS);
  10. BEGIN
  11. OPEN C;
  12. WHILE (1 = 1) DO
  13. BEGIN
  14. FETCH C INTO :RNAME;
  15. IF (ROW_COUNT = 0) THEN
  16. LEAVE;
  17. SUSPEND;
  18. END
  19. CLOSE C;
  20. END^
  21. SET TERM ;^

See also

DECLARE CURSOR, OPEN, CLOSE