statement

OPEN

Opens a refcursor in PL/pgSQL. PostgreSQL top-level DECLARE already opens its cursor; OPEN here is procedural, not a top-level SQL command. Example target: PostgreSQL 17 PL/pgSQL. Use only the isolated starter dataset; setup and expected results are in the batch evidence.

Formal Syntax & Structure

sql syntaxsql
OPEN cursor_name FOR query

Working Example

Example Snippetsql
DO $$ DECLARE c refcursor; r integer; BEGIN OPEN c FOR SELECT 7; FETCH c INTO r; IF r <> 7 THEN RAISE EXCEPTION 'unexpected row'; END IF; CLOSE c; END; $$;
SQL Reference