Skip to content
DB2 & data

Cursor

A mechanism for stepping through a set of rows one at a time, when a query returns more than one result.

Also written DECLARE CURSOR, FETCH

A cursor solves a mismatch between SQL and COBOL. SQL returns sets of rows; a COBOL program processes one record at a time. A cursor bridges the two.

The pattern has four parts. The cursor is declared with the query it represents. It is opened, which runs the query. Then rows are fetched one at a time into host variables, each fetch handing the program the next row. When the rows are exhausted, the fetch reports it, and the cursor is closed.

That loop is one of the most common shapes in mainframe application code: open, fetch until no more rows, process each one, close.

Cursors can be declared for update, allowing the program to change or delete the row it is currently positioned on. Scrollable cursors can move backwards and jump around rather than only moving forward.

A single-row query does not need one. A plain SELECT INTO retrieves directly into host variables and reports an error if more than one row matches. Cursors are for when the answer is a set.

Browse all 115 terms

Learn this properly.

Use Cursor for real in Mainframe101, in your browser, with Zed beside you. Join the waitlist.

Early access and updates. No spam, unsubscribe any time.