0

I am getting an error for the below FOR loop with cursor in a function:

ERROR: syntax error at or near "AS"
CREATE OR REPLACE FUNCTION functionName(custom varchar(15)) RETURNS INTEGER AS $$

DECLARE 
...

BEGIN
...
    FOR loop AS cursor CURSOR FOR 
            SELECT column FROM table
    DO
    ...
    END FOR;
    RETURN someValue;
END;
$$
LANGUAGE plpgsql;
1
  • 1
    Where in the manual did you find that syntax? Commented Nov 17, 2015 at 9:24

1 Answer 1

1

This is wrong syntax - Postgres doesn't support declaration of CURSOR inside FOR statement. See documentation:

CREATE OR REPLACE FUNCTION foo()
RETURNS void AS $$
DECLARE r record;
BEGIN
   FOR r IN SELECT xx,yy FROM some_tab
   LOOP
     RAISE NOTICE 'row data: %', r;
   END LOOP;
END;
$$ LANGUAGE plpgsql;

It looks so you are using ANSI SQL PSM syntax. PL/pgSQL is based on PL/SQL syntax (Oracle/ADA).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.