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.
Related terms
- Embedded SQLSQL statements written directly inside a COBOL program, marked off so they can be prepared before compiling.
- SQLThe language used to query and change relational data. The same SQL used everywhere else, running against mainframe tables.
- SQLCODEThe result code DB2 returns after every SQL statement, saying whether it worked and what happened if it did not.
- COBOLThe programming language most business logic on the mainframe is written in. Verbose by design, and still running the world's payments.
- DB2 for z/OSIBM's relational database on the mainframe, where a great deal of the world's banking and insurance data is stored.