SQLMentor // glossary

Cursor

A cursor is a pointer that lets procedural code step through the rows of a query result one at a time. It is used when per-row logic can't be expressed in a single set-based statement.

An implicit cursor is created automatically for a statement; an explicit cursor is declared, opened, fetched from in a loop, and closed. Cursors are essential in PL/SQL and T-SQL for row-by-row processing.

Because row-by-row work is far slower than set-based SQL, prefer a single UPDATE, MERGE, or INSERT ... SELECT whenever the logic can be expressed that way.

Example

DECLARE
  CURSOR c IS SELECT employee_id, salary FROM employees;
BEGIN
  FOR rec IN c LOOP
    DBMS_OUTPUT.PUT_LINE(rec.employee_id || ': ' || rec.salary);
  END LOOP;
END;