IBM Books

SQL Reference

DECLARE CURSOR

The DECLARE CURSOR statement defines a cursor.

Invocation

Although an interactive SQL facility might provide an interface that gives the appearance of interactive execution, this statement can only be embedded within an application program. It is not an executable statement and cannot be dynamically prepared.

Authorization

The term "SELECT statement of the cursor" is used in order to specify the authorization rules. The SELECT statement of the cursor is one of the following:

For each table or view identified (directly or using an alias) in the SELECT statement of the cursor, the privileges held by the authorization ID of the statement must include at least one of the following:

If statement-name is specified:

If select-statement is specified:

Syntax

>>-DECLARE--cursor-name--CURSOR----+------------+--------------->
                                   '-WITH HOLD--'
 
>-----+-------------+--FOR--+-select-statement-+---------------><
      '-WITH RETURN-'       '-statement-name---'
 

Description

cursor-name
Specifies the name of the cursor created when the source program is run. The name must not be the same as the name of another cursor declared in the source program. The cursor must be opened before use (see OPEN).

WITH HOLD
Maintains resources across multiple units of work. The effect of the WITH HOLD cursor attribute is as follows:

WITH RETURN
Option is available for applications that require compatibility with DB2 Universal Database for OS/390. This option should only be used when declaring a cursor in the code for a stored procedure. When specified, it is ignored by DB2 Universal Database.

select-statement
Identifies the SELECT statement of the cursor. The select-statement must not include parameter markers, but may include references to host variables. The declarations of the host variables must precede the DECLARE CURSOR statement in the source program. See select-statement for an explanation of select-statement.

statement-name
The SELECT statement of the cursor is the prepared SELECT statement identified by the statement-name when the cursor is opened. The statement-name must not be identical to a statement-name specified in another DECLARE CURSOR statement of the source program.

For an explanation of prepared SELECT statements, see PREPARE.

Notes

Example

The DECLARE CURSOR statement associates the cursor name C1 with the results of the SELECT.

   EXEC SQL DECLARE C1 CURSOR FOR
      SELECT DEPTNO, DEPTNAME, MGRNO
      FROM DEPARTMENT
      WHERE ADMRDEPT = 'A00';


Footnotes:

79
The FOR READ ONLY clause is defined in the read-only-clause.

80
The FOR UPDATE clause is defined in the update-clause.


[ Top of Page | Previous Page | Next Page | Table of Contents | Index ]

[ DB2 List of Books | Search the DB2 Books ]