1

I am trying to write a function in postgresql and i am getting a syntax error for the if statement:

CREATE OR REPLACE FUNCTION getallcampuses(IN a character varying, IN b character varying)
  RETURNS TABLE(campusid charactervarying, campusname character varying) AS
$BODY$
BEGIN
if $1 = 'PREK' then
     SELECT * from "SIS_campus";
ELSE
    BEGIN
       IF $2 = 'DAll' then
           SELECT distinct(district_id) || 'ALL' AS CampusID, ' District' AS CampusName

           UNION

           SELECT campus_id, name
           FROM   "SIS_campus"
           WHERE  district_name IS NOT NULL
           order by name;
       Elsif $2 = 'All' then

           SELECT campus_id, name
           FROM   "SIS_campus"
           WHERE  district_name IS NOT NULL
           and isnumeric(name) = 0
           order by name;
        end if;
    END
end if;
END
$BODY$
LANGUAGE sql;

Here is the error:

ERROR:  syntax error at or near "if"
LINE 5: if $1 = 'PREK' then
        ^
2
  • You have a typo at Elsif $2 = 'All' then Commented Sep 12, 2014 at 16:48
  • 1
    Thanks for including a complete function body and the exact error message text. That's so rare, and is appreciated. (In future, please put your PostgreSQL version in the text too, though). Commented Sep 12, 2014 at 16:52

1 Answer 1

2

The function body is PL/PgSQL, but you declared it LANGUAGE sql.

Use LANGUAGE plpgsql if you're writing PL/PgSQL.

(related prior answer)

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.