CURSOR

Function

CURSOR defines a cursor. This command retrieves few rows of data in a query.

To process SQL statements, the stored procedure process assigns a memory segment to store context association. Cursors are handles or pointers to context regions. With cursors, stored procedures can control alterations in context regions.

Precautions

Syntax

1
2
3
CURSOR cursor_name
    [ BINARY ]  [ NO SCROLL ]  [ { WITH | WITHOUT } HOLD ]
    FOR query ;

Parameter Description

Examples

Start a transaction:

1
START TRANSACTION;

Create a cursor named cursor1:

1
CURSOR cursor1 FOR SELECT * FROM tpcds.customer_address ORDER BY 1;

create a cursor named cursor2:

1
CURSOR cursor2 FOR VALUES(1,2),(0,3) ORDER BY 1;

An example of using the WITH HOLD cursor is as follows:

  1. Set up a WITH HOLD cursor.
    1
    DECLARE cursor3 CURSOR WITH HOLD FOR SELECT * FROM tpcds.customer_address ORDER BY 1;
    
  2. Fetch the first two rows from cursor3.
    1
    FETCH FORWARD 2 FROM cursor3;
    
  3. End the transaction.
    1
    END;
    
  4. Fetch the next row from cursor3.
    1
    FETCH FORWARD 1 FROM cursor3;
    
  5. Close a cursor.
    1
    CLOSE cursor3;
    

Helpful Links

FETCH