8. OpenSQL Statements : FETCH : Examples: Fetch
 
Share this page                  
Examples: Fetch
1. Typical fetch, with associated cursor statements.
exec sql begin declare section;
  name  character_string(20);
  age   integer;
  exec sql end declare section;
exec sql declare cursor1 cursor for
  select ename, age
  from employee
  order by ename;
...
exec sql open cursor1;
 
loop until no more rows
  exec sql fetch cursor1
    into :name, :age;
  print name, age;
end loop;
exec sql close cursor1;
Assuming the structure:
emprec
  name    character_string(20),
  age    integer;
the fetch in the above example could have been written
exec sql fetch cursor1
  into :emprec;
The preprocessor would then interpret that statement as though it had been written
exec sql fetch cursor1
  into :emprec.name, :emprec.age;
2. Fetch using an indicator variable.
exec sql fetch cursor2 into :name,
:salary:indicator_var;