INTO Clause with Cursors

If the SELECT statement returns more than one row, you must use a cursor in a FETCH statement to fetch the rows individually. You can put the INTO clause in the FETCH statement rather than in the SELECT statement, but you should not put it in both.

The following code examples show different ways you can use the INTO clause. As both examples show, first you must use the DECLARE statement to declare a cursor.

The following example show how to use the INTO clause in the SELECT statement:

EXEC SQL declare q_curs cursor for
   select lname, company
      into :p_lname, :p_company
      from customer;
EXEC SQL open q_curs;
while (SQLCODE == 0)
   EXEC SQL fetch q_curs;
EXEC SQL close q_curs;

The following statement shows how to use the INTO clause in a FETCH statement:

EXEC SQL declare q_curs cursor for
   select lname, company from customer;
EXEC SQL open q_curs;
while (SQLCODE == 0)
   EXEC SQL fetch q_curs into :p_lname, :p_company;
EXEC SQL close q_curs;