Question

I'm practicing a little bit with postgreSQL, i'm creating a very simple function that inserts a row into a table depending on the value of the variable 'num'. However, when I try to create the function I get the following error in pgAdmin III:

"An error has occured: ERROR: syntax error at or near IF LINE 3: IF num = 1 THEN"

This is my code:

    CREATE FUNCTION "elseIf"(IN num integer) RETURNS void AS
    $BODY$
    IF num = 1 THEN

        INSERT INTO "Accounts"(
         "Email", "Password")
           VALUES ('email1', 'password1');

    ELSE
        INSERT INTO "Accounts"(
         "Email", "Password")
           VALUES ('email2', 'password2');

    END IF;
    $BODY$
    LANGUAGE plpgsql VOLATILE NOT LEAKPROOF;
    ALTER FUNCTION public."elseIf"(IN integer)
    OWNER TO repository;

Any possible solution? thanks in advance!

Was it helpful?

Solution

You forgot the BEGIN.

PL/PgSQL must be wrapped in a BEGIN ... END block.

... RETURNS VOID AS
$BODY$
BEGIN
   IF num = 1 THEN
      ...
   END IF;
END;
$BODY$ ....
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top