sqladvanced

Cursor - Technique 6

Work with database cursors

sql
DO $$
DECLARE
  rec RECORD;
  c CURSOR FOR SELECT id, total FROM invoices WHERE total > 1000;
BEGIN
  OPEN c;
  LOOP
    FETCH c INTO rec;
    EXIT WHEN NOT FOUND;
    RAISE NOTICE 'Invoice % => %', rec.id, rec.total;
  END LOOP;
  CLOSE c;
END;
$$;

Use Cases

  • database operations
  • data management

Tags

Related Snippets

Similar patterns you can reuse in the same workflow.