Valid in: ESQL
The Endselect statement terminates embedded SQL select loops. A select loop is a block of code delimited by begin and end statements and associated with a select statement. As the select statement retrieves rows from the database, each row is processed by the code in the select loop. For more information about select loops, see Select Loops.
When the endselect statement is executed, the program stops retrieving rows from the database and program control is transferred to the first statement following the select loop.
The endselect statement must be inside the select loop that it is intended to terminate. If an endselect statement is placed inside a forms code block that is syntactically nested within a select loop, the statement ends the code block as well as the select loop.
The statement must be terminated according to the rules of the host language.
Note: To find out how many rows were retrieved before the endselect statement was issued, check the sqlerrd(3) variable of the SQLCA. For details about the SQLCA, see the chapter "Working with Embedded SQL."
The Endselect statement has the following format:
EXEC SQL ENDSELECT;
All users are permitted to use this statement.
If autocommit is off (default behavior), the Endselect statement does not affect locking. All locks held before the Endselect statement remain. If autocommit is on, the Endselect statement ends the query and locks are dropped.
The following example breaks out of a select loop on a data loading error:
exec sql select ename, eno into :ename, :eno
from employee;
exec sql begin;
load ename, eno into data set;
if error then
print 'Error loading ', ename, eno;
exec sql endselect;
end if
exec sql end;
/* endselect transfers control to here */