Declares a cursor for processing multiple rows returned by a select statement.
exec sql declare cursor_name cursor for select_statement [for update [of col_name_1 [, col_name_n]…]| for read only];
The cursor’s name, used to reference the cursor in open, fetch, and close statements. A cursor’s name must be unique on each connection and must have no more than 128 characters.
The Transact-SQL select statement to be executed when the cursor is opened. See the description of the select statement in the Adaptive Server Enterprise Reference Manual for more information.
Specifies that the cursor’s result list can be updated. (To update the result list, you use the update statement.)
The name of the first column to be updated.
The name of the nth column to be updated.
Specifies that the cursor’s result list cannot be updated.
main()
{
exec sql begin declare section;
CS_CHAR b_titleid[TIDSIZE+1];
CS_CHAR b_title[65];
CS_CHAR b_type[TYPESIZE+1];
exec sql end declare section;
long SQLCODE;
exec sql connect “sa”;
exec sql use pubs2;
exec sql declare titlelist cursor for
select title_id, substring(title,1,64)
from titles where type like :b_type;
strcpy(b_type, "business");
exec sql open titlelist;
for (;;)
{
exec sql fetch titlelist into :b_titleid,
:b_title;
if (SQLCODE == 100)
break;
printf(" %-8s %s\n", b_titleid, b_title);
}
exec sql close titlelist;
exec sql disconnect all;
}
The Embedded SQL precompiler generates no code for the declare cursor statement.
The select_statement does not execute until your program opens the cursor by using the open cursor statement.
The syntax of the select_statement is identical to that shown in the Adaptive Server Enterprise Reference Manual, except that you cannot use the compute clause in Embedded SQL.
The select_statement can contain host variables. The values of the host variables are substituted when your program opens the cursor.
If you omit either the for update or read only clause, Adaptive Server determines whether the cursor is updatable.
close, connect, deallocate cursor, declare cursor (stored procedure), declare cursor (dynamic), fetch, open, update