Skip to content
Advertisement

Postgres syntax error at or near “IF”

I am new to postgres and I am working on an assignment of mine. I had to create a table with only 1 column and, then I was given this statement to run in on pgadmin III:

when I run this I get this error: syntax error at or near “IF”

I have already take a look at this 38.6.2. Conditionals38.6.2. Conditionals , I dont understand this very well, Do I need to change the query to have

and then when it ends I should end it with:

Why there is an error after all??

Advertisement

Answer

IF and other PL/pgSQL features are only available inside PL/pgSQL functions. You need to wrap your code in a function if you want to use IF. If you’re using 9.0+ then you can do use DO to write an inline function:

If you’re using an earlier version of PostgreSQL then you’ll have to write a named function which contains your code and then execute that function.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement