SQL Language Guide : 8. SQL Statements : OPEN : OPEN Examples
 
Share this page                  
OPEN Examples
1. Declare and open a cursor.
exec sql declare cursor1 cursor for
    select :one + 1, ename, age
    from employee
    where age :minage;
...
exec sql open cursor1;
When the OPEN statement is encountered, the variables, one and minage, are evaluated. The first statement that follows the opening of a cursor must be a FETCH statement to define the cursor position and retrieve data into the indicated variables:
exec sql fetch cursor1
    into :two, :name, :age;
The value of the expression, :one + 1, is assigned to the variable, two, by the fetch.
2. The following example demonstrates the dynamic SQL syntax. In a typical application the prepared statement and its parameters are constructed dynamically.
select_buffer =
    'select * from employee where eno = ?';
exec sql prepare select1 from :select_buffer;
exec sql declare cursor2 cursor for select1;
eno = 1234;
exec sql open cursor2 using :eno;