skip to main content

Embedded SQL Statements : FETCH

FETCH
The FETCH statement retrieves a row from a cursor.
General format
EXEC SQL [AT Database]
  FETCH { NEXT     } FROM { Cursor-Name } [ INTO Host-Variable, ... ]
        { PRIOR    }]
        { FIRST    }
        { LAST     }
  END-EXEC
Syntax rules
1.
Cursor-Name is a Nonnumeric Literal, as defined in the Definitions section of the Preface of this document.
2.
Host-Var must be USAGE HANDLE
3.
4.
PREVIOUS and PRIOR are synonymous.
General rules
1.
Cursor-Name must be previously defined by a DECLARE statement.
2.
When Host-Variable is a host variable declared as a group-item, the runtime can use all subordinate items as separate values instead of using the group-item as a single value. To enable this feature, compile with the -cghv option.
3.
When the runtime framework property iscobol.jdbc.cursor.type * is set to its default value 1 (FORWARD_ONLY), moving backward is not allowed.
4.
Database identifies the active connection that will execute the query and must be previously defined using a Format 4 DECLARE statement.
5.
Examples
Format 1 - Declare, open and fetch a cursor
exec sql 
   declare cust_cur  cursor for select * from customers
end-exec               
exec sql 
   open cust_cur
end-exec     
perform until 1 = 2
   exec 
      sql fetch next cust_cur into :ws-cust-code, :ws-cust-name
   end-exec
   display "code: " ws-cust-code " name: " ws-cust-name
   if sqlcode = 100
      exit perform
   end-if
exec sql 
   close cust_cur

Copyright (c) 2017 Veryant
Contact us
Please share your comments on this manual or on any
Veryant product documentation with the email button at the top left