DECLARE CURSOR Statement [ESQL] [SP]

Declares a cursor. Cursors are the primary means for manipulating the results of queries.

Quick Links:

Go to Parameters

Go to Examples

Go to Usage

Go to Standards

Go to Permissions

Syntax

DECLARE cursor-name SCROLL
     | NO SCROLL 
     | DYNAMIC SCROLL 
   ]
   CURSOR FORselect-statement FOR for-clause
   | statement-name   
   | USING variable-name }

for-clause
   READ ONLY | UPDATE

Parameters

(back to top)

Examples

(back to top)

Usage

(back to top)

The DECLARE CURSOR statement declares a cursor with the specified name for a SELECT statement or a CALL statement.

Embedded SQL statements are named using the PREPARE statement. Cursors can be declared only for a prepared SELECT or CALL.

SAP Sybase IQ supports updatable cursors on single tables.

SAP Sybase IQ supports one type of cursor sensitivity, which is defined in terms of which changes to underlying data are visible. All SAP Sybase IQ cursors are asensitive, which means that changes might be reflected in the membership, order, or values of the result set seen through the cursor, or might not be reflected at all.

With an asensitive cursor, changes effected by positioned UPDATE and positioned DELETE statements are visible in the cursor result set, except where client-side caching prevents seeing these changes. Inserted rows are not visible.

Rows that are updated so that they no longer meet the requirements of the WHERE clause of the open cursor are still visible.

When using cursors, there is always a trade-off between efficiency and consistency. Asensitive cursors provide efficient performance at the expense of consistency.

LONG VARCHAR and LONG BINARY data types are not supported in updatable cursors.

Scalar user-defined functions and user-defined aggregate functions are not supported in updatable cursors.

Supported query specifications for updatable cursors in SAP Sybase IQ are:
  • Expressions in the select list against columns that are not functionally dependent on columns being updated
  • Arbitrary subqueries with asensitive behavior, that is, changes to data referenced by subqueries are not visible in the cursor result set
  • ORDER BY clause; the ORDER BY columns may be updated, but the result set does not reorder
  • Columns that meet these requirements:
    • No CAST on a column
    • Base columns of a base table in the SELECT clause
    • There are no expressions or functions on that column in the SELECT clause and it is not duplicated in the select list (for example, SELECT c1, c1).
    • Base columns of a base table restricted to those listed in the FOR UPDATE OF column-name-list clause, if the clause is specified.
SAP Sybase IQ does not permit updatable cursors on queries that contain any operator that precludes a one-to-one mapping of result set rows to rows in a base table; specifically:
  • SELECT DISTINCT
  • Operator that has a UNION
  • Operator that has a GROUP BY
  • Operator that has a SET function
  • Operator that has an OLAP function, with the exception of RANK()

See the description of the UPDATE (positioned) Statement [ESQL] [SP] for information on the columns and expressions allowed in the SET clause for the update of a row in the result set of a cursor.

SAP Sybase IQ supports inserts only on updatable cursors where all nonnullable, nonidentity columns are both selected and updatable.

In SAP Sybase IQ, COMMIT and ROLLBACK are not allowed inside an open updatable cursor, even if the cursor is opened as a hold cursor. SAP Sybase IQ does support ROLLBACK TO SAVEPOINT inside an updatable cursor.

Any failure that occurs after the cursor is open results in a rollback of all operations that have been performed through this open cursor.

Updatable Cursor Limitations

A declared cursor is read-only and not updatable in cases where:
  • The data extraction facility is enabled with the TEMP_EXTRACT_NAME1 option set to a pathname
  • ANSI_CLOSE_CURSORS_ON_ROLLBACK is set OFF
  • CHAINED is set OFF
  • The statement is INSERT SELECT or SELECT INTO
  • More than one table is included
  • No updatable columns exist

If SAP Sybase IQ fails to set an updatable cursor when requested, see the .iqmsg file for related information.

There is a limitation regarding updatable cursors and ODBC. A maximum of 65535 rows or records can be updated, deleted, or inserted at a time using these ODBC functions:
  • SQLSetPos SQL_UPDATE, SQL_DELETE, and SQL_ADD
  • SQLBulkOperations SQL_ADD, SQL_UPDATE_BY_BOOKMARK, and SQL_DELETE_BY_BOOKMARK

There is an implementation-specific limitation to the maximum value in the statement attribute that controls the number of effected rows to the largest value of an UNSIGNED SMALL INT, which is 65535.

SQLSetStmtAttr(HANDLE,SQL_ATTR_ROW_ARRAY_SIZE, VALUE,0)
SAP Sybase IQ updatable cursors differ from ANSI SQL3 standard behavior as follows:
  • Hold cursor update close on commit.
  • SAP Sybase IQ locks tables when the cursor is open.
  • All updates, deletes, and insert operations are applied when the cursor is closed, in this order: deletes first, then updates, then inserts.
Note: Use the sp_iqcursorinfo system procedure to display detailed information about cursors currently open on the server.

Standards

(back to top)

  • SQL—Vendor extension to ISO/ANSI SQL grammar.
  • SAP Sybase Database product—Supported by Open Client/Open Server.

Permissions

(back to top)

None

Related reference
CALL Statement
DELETE (positioned) Statement [ESQL] [SP]
FETCH Statement [ESQL] [SP]
OPEN Statement [ESQL] [SP]
PREPARE Statement [ESQL]
SELECT Statement
UPDATE (positioned) Statement [ESQL] [SP]