0

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!

1 Answer 1

1

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$ ....
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.