Entering content frame

 OPEN CURSOR Statement 

Use

You can use the OPEN CURSOR statement to execute a dynamic SQL statement with parameters, and generate a named result table (cursor).

Prerequisites

·        You have used the PREPARE statement to prepare the SQL statement for execution.

·        If you are using a descriptor, you have initialized it with the DESCRIBE statement.

·        You have already defined the cursor (the named result table) with a DECLARE CURSOR statement.

Syntax

EXEC SQL [<session_spec>] [<for_clause>] OPEN <cursor_name>
         [ USING <parameter_list>
         | USING DESCRIPTOR [<descriptor name>] [KEEP]
         | INTO <parameter_list>
         | INTO DESCRIPTOR [<descriptor name>] [KEEP]]

·        If, in the programming phase, you do not know how many different result tables are processed by your application program, you can specify the result table name <cursor_name> in a host variable in the OPEN CURSOR statement.

·         Specify KEEP if you want to follow the OPEN CURSOR statement with PUTVAL statements.

·        Here, INTO has the same meaning as specifying USING <parameter_list>. It guarantees compatibility with other database systems.

EXEC SQL BEGIN DECLARE SECTION;

int hno;

char name[16];

float price;

char cmd[100];

char *stmt;

char *cursor;

EXEC SQL END DECLARE SECTION;

strcpy (cmd, "INSERT (INTO???) hotel VALUES(10, 'Excelsior', \
79000, 'Atlanta', 135.00)");

EXEC SQL PREPARE STMT1 FROM :cmd;

EXEC SQL DECLARE cur1 CURSOR FOR STMT1;

EXEC SQL OPEN cur1;

hno = 10;

strcpy (cmd, "SELECT name, price FROM hotel WHERE hno=?");

stmt = "STMT2";

EXEC SQL PREPARE :stmt FROM :cmd;

cursor = "cur2";

EXEC SQL DECLARE :cursor CURSOR FOR :stmt;

EXEC SQL OPEN :cursor USING :hno;

EXEC SQL FETCH :cursor INTO :name, :price;

 

 

Leaving content frame