SQL Keywords

SQL CURRENT

What does the SQL keyword CURRENT do?

CURRENT identifies the row currently pointed to by a cursor (WHERE CURRENT OF) and marks the present row in window-frame clauses (CURRENT ROW).
Sign up to get up to date news on SQL keywords
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.

Compatible dialects for SQL CURRENT:

SQL CURRENT Full Explanation

CURRENT is a reserved Standard SQL keyword that refers to the “row in focus” of an executing statement. It shows up in two main contexts:1. Positioned UPDATE or DELETE • Syntax: WHERE CURRENT OF cursor_name • Meaning: Modify or remove the single row most recently fetched by the named cursor. • Requirements: The cursor must be declared FOR UPDATE/DELETE, opened, and positioned on a row. If the cursor is closed or not positioned, the statement raises SQLSTATE 24000. • Benefits: Eliminates the need to repeat a full primary-key predicate, reducing race conditions while looping through result sets.2. Window-frame specifications • Example: ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW • Meaning: Include the current row in the frame boundary so analytic functions (SUM, AVG, etc.) are calculated up to that row.The keyword cannot be selected or assigned directly and has no parameters by itself. It always appears as part of a larger clause (CURRENT OF, CURRENT ROW).

SQL CURRENT Syntax

-- Positioned update/delete
UPDATE table_name
SET column = value
WHERE CURRENT OF cursor_name;

DELETE FROM table_name
WHERE CURRENT OF cursor_name;

-- Window frame
... OVER (
  ORDER BY sort_col
  ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
)

SQL CURRENT Parameters

  • cursor_name (identifier) - Name of an open, updatable cursor currently positioned on a row

Example Queries Using SQL CURRENT

-- Example 1: positioned UPDATE
BEGIN;
DECLARE emp_cur CURSOR FOR
  SELECT id, salary FROM employees FOR UPDATE;
OPEN emp_cur;
FETCH emp_cur;             -- positions cursor on first row
UPDATE employees
SET salary = salary * 1.05
WHERE CURRENT OF emp_cur;  -- gives 5% raise to fetched employee
CLOSE emp_cur;
COMMIT;

-- Example 2: running total window frame
SELECT
  order_id,
  order_date,
  SUM(amount) OVER (
    ORDER BY order_date
    ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
  ) AS running_total
FROM orders
ORDER BY order_date;

Expected Output Using SQL CURRENT

  • Example 1 updates exactly one employee row – the one the cursor emp_cur last fetched
  • Example 2 returns each order with a running_total column that accumulates amounts from the first row through the current row

Use Cases with SQL CURRENT

  • Cursor-based record processing that needs safe, row-by-row updates or deletes.
  • Writing window functions that calculate running totals, moving averages, or cumulative counts up to the present row.

Common Mistakes with SQL CURRENT

  • Forgetting to declare the cursor FOR UPDATE – the positioned UPDATE/DELETE will fail.
  • Attempting WHERE CURRENT OF when the cursor is closed or before the first FETCH.
  • Expecting MySQL or SQLite to support WHERE CURRENT OF – they do not.
  • Confusing CURRENT with the keyword CURRENT_DATE or CURRENT_TIMESTAMP, which return temporal values, not row references.

Related Topics

First Introduced In

SQL-92

Frequently Asked Questions

When should I use WHERE CURRENT OF?

Use it when looping through a cursor and you need to update or delete the exact row you just fetched, without rewriting a WHERE clause based on primary keys.

Does MySQL support WHERE CURRENT OF?

No. MySQL cursors exist only in stored procedures and do not implement positioned UPDATE or DELETE. Use a regular UPDATE with a key predicate instead.

How is CURRENT ROW related to CURRENT?

CURRENT ROW is a window-frame boundary that includes the present row in calculations. It uses the same underlying concept – referencing the row currently being processed.

What happens if the cursor is not positioned?

The database raises an invalid cursor state error (SQLSTATE 24000). The cursor must be open and a FETCH must have been executed before WHERE CURRENT OF.

Sign up to get up to date news on SQL keywords
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.
Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie Logo
Bauhealth Logo
Truvideo Logo

Check out other commonly used SQL Keywords!