fetch

Description

Copies data values from the current cursor row into host variables or a dynamic descriptor.

Syntax

exec sql [at connection_name] fetch [rebind | norebind] cursor_name
into {:host_variable [[indicator]:indicator_variable]
 [,:host_variable 
 [[indicator]:indicator_variable]]… | 
 descriptor descriptor_name | 
 sql descriptor descriptor_name};

Parameters

rebind | norebind

Specifies whether host variables require rebinding for this fetch statement. The rebind clause overrides precompiler options that control rebinding.

cursor_name

The name of the cursor. The name is defined in a preceding declare cursor statement.

host_variable

A host language variable defined in a declare section.

indicator_variable

A 2-byte host variable declared in a previous declare section. If the value for the associated variable is null, fetch sets the indicator variable to -1. If truncation occurs, fetch sets the indicator variable to the actual length of the result column. Otherwise, it sets the indicator variable to 0.

descriptor

Identifies descriptor_name as a SQLDA structure.

sql descriptor

Identifies descriptor_name as a SQL descriptor.

descriptor_name

The name of the dynamic descriptor that will hold the result set.

Examples

Example 1

exec sql begin declare section; 
     CS_CHAR        title_id[6]; 
     CS_CHAR        title[80]; 
     CS_CHAR        type[12]; 
     CS_SMALLINT    i_title; 
     CS_SMALLINT    i_type; 
 exec sql end declare section;
 exec sql declare title_list cursor for      
     select type, title_id, title from titles
     order by type;
 
 exec sql open title_list
 while (sqlca.sqlcode != 100) {
exec sql fetch title_list into
         :type :i_type, :title_id, :title :i_title; 

         if (i_type != -1) {
             printf("Type: %s\n", type); 
         }
         else {
             printf("Type: undecided\n"); 
         }
 
         printf("Title id: %s\n", title_id);  
 
         if (i_title <> -1) {
             print "Title: ", title; 
         }
         else {
             print "Title: undecided"; 
         }
 }
 
 exec sql close title_list; 

Usage

See also

allocate descriptor, close, declare, delete (positioned cursor), open, prepare, update