1

I am new to POSTGRES and currently exploring how to create and execute stored procedures. I was trying a simple example with conditional constructs and got stuck with an error :(

create procedure learnpostgre(x integer, y integer)
language plpgsql
as $$
if x > y
then
raise 'X is greater'
end if
$$;

The error I am getting is 'syntax error at or near if'. I saw similar issues on SO and tried wrapping my code block under do block but faced the same error message for 'Do'. I am running my code in pgadmin4 and postgres version is 14.X

1
  • 1
    As documented in the manual you need a begin ... end block in PL/pgSQL Commented Feb 4, 2022 at 15:12

1 Answer 1

1

You're missing begin and end and a few semicolons. This should work:

create procedure learnpostgre(x integer, y integer)
language plpgsql
as $$
begin
if x > y
then
raise 'X is greater';
end if;
end
$$
Sign up to request clarification or add additional context in comments.

1 Comment

it worked. Thanks. I guess I am still confusing it with sql syntax.

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.