Reputation: 93
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!
Upvotes: 0
Views: 460
Reputation: 324375
PL/PgSQL must be wrapped in a BEGIN ... END
block.
... RETURNS VOID AS
$BODY$
BEGIN
IF num = 1 THEN
...
END IF;
END;
$BODY$ ....
Upvotes: 1