Repositions a cursor, and retrieves data from it.
Synopsis
FETCH cursor-name [INTO host-variable-list]
Within an embedded SQL application, a FETCH statement retrieves data
from a
cursor.
As an SQL statement, this is supported only from within embedded
SQL. Equivalent operations are supported through ODBC using the ODBC
API.
The INTO clause allows data from the columns of a fetch to be placed into local variables.
Each variable in the list, from left to right, is associated with the corresponding column
in the cursor result set. The data type of each variable must either match or be a
supported implicit conversion of the data type of the corresponding result set column.
The number of variables must match the number of columns in the cursor select list.
The FETCH operation also sets the %ROWCOUNT local variable to the number
of fetched rows.
The following example shows a FETCH statement retrieving data from a cursor named EmpCursor:
&sql(DECLARE EmpCursor CURSOR FOR SELECT EmpId,ForeName,SurName
INTO :empid,:fname,:sname FROM Employees)
&sql(OPEN EmpCursor)
&sql(FETCH EmpCursor)
&sql(CLOSE EmpCursor)
The following example shows a FETCH statement with the INTO clause:
&sql(DECLARE EmpCur CURSOR FOR
SELECT SurName,Telephone
FROM Employees WHERE SurName="Johnson")
&sql(OPEN EmpCur)
&sql(FETCH EmpCur INTO :name, :tel)
The following example shows a FETCH statement with the INTO clause that overrides the INTO
clause of the corresponding DECLARE:
&sql(DECLARE EmpCur CURSOR FOR
SELECT SurName,Telephone INTO :name,:tel
FROM Employees WHERE SurName="Johnson")
&sql(OPEN EmpCur)
&sql(FETCH EmpCur INTO :n1, :t1)