declare cursor (static)

Description

Declares a cursor for processing multiple rows returned by a select statement.

Syntax

exec sql declare cursor_name 
 cursor for select_statement 
 [for update [of col_name_1 [, col_name_n]…]|
 for read only]; 

Parameters

cursor_name

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 255 characters.

select_statement

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.

for update

Specifies that the cursor’s result list can be updated. (To update the result list, you use the update statement.)

of col_name_1

The name of the first column to be updated.

of col_name_n

The name of the nth column to be updated.

for read only

Specifies that the cursor’s result list cannot be updated.

Examples

Example 1

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;
}

Usage

See also

close, connect, deallocate cursor, declare cursor (stored procedure), declare cursor (dynamic), fetch, open, update