SQL CURSOR (Explicit Cursor)
SQL CURSOR (Explicit Cursor) is a SQL statement in the DML Statements category. Explicit cursors allow row-by-row processing when set-based operations are not feasible. The syntax is DECLARE cur CURSOR FOR SELECT ...; OPEN cur; FETCH cur INTO ...; CLOSE cur;. It returns cursor processed. A typical example: DECLARE CURSOR emp_cur IS SELECT id, name, salary FROM employees WHERE department_id = 20; v_id employees.id%TYPE; v_name employees.name%TYPE; v_salary employees.salary%TYPE; BEGIN OPEN emp_cur; LOOP FETCH emp_cur INTO v_id, v_name, v_salary; EXIT WHEN emp_cur%NOTFOUND; -- Process each row UPDATE employees SET salary = v_salary * 1.1 WHERE id = v_id; END LOOP; CLOSE emp_cur; COMMIT; END; -- Cursor attributes: -- %FOUND, %NOTFOUND, %ROWCOUNT, %ISOPEN -- FOR loop cursor (simpler): FOR emp… A close relative is INSERT INTO, which inserts new rows into a table. A close relative is UPDATE, which modifies existing rows in a table. Always use WHERE to avoid updating all rows.