pgAdmin III query tool "IF" statement ERROR

Posted on 2007-10-12
Last Modified: 2016-09-10
This is giving me an error in the pgAdmin II query tool.  

IF EXISTS(SELECT relname FROM pg_class WHERE relname = 'Angelina_VR_Data') THEN
      SELECT 2
      SELECT 1

The error is:  

ERROR: syntax error at or near "IF"
SQL state: 42601
Character: 1

Thank you in advance for your help.  Please Help! This is driving me crazy!!!
Question by:FreightTrain
    LVL 22

    Accepted Solution

    if then else endif is PLPGSQL syntax not SQL syntax.  The equivalent is the CASE WHEN THEN ELSE END construct. eg.

    testdb=> select case when exists(select 1) then 2 else 1 end;
    (1 row)

    select case when EXISTS(SELECT relname FROM pg_class WHERE relname = 'Angelina_VR_Data') THEN 2 else 1 end;

    Author Comment

    Two Words: YOU ROCK!!!  Thanks again earthman2!!!  This is so easy, once you know!!!

    Expert Comment

    by:Vishakha Tanna
    Hello...I want to check if count if my count is greater than 0 and based on it i want to return. I am trying
    select case when exists(count IN (select count(*) from outputnodeoss AS count) > 0) then 2 else 1 end;

    but getting error

    ERROR:  syntax error at or near "count"
    LINE 1: select case when exists(count IN (select count(*) from outpu...

    ********** Error **********

    ERROR: syntax error at or near "count"
    SQL state: 42601
    Character: 25

    Please guide
    LVL 22

    Expert Comment

    by:earth man2
    Vishakna Tanna, you need to open a new question rather than tag yours onto the end of another.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    Best database to use for Maps is PostgreSQL. This is an open source database. Comes as a package with most Linux OS. For more info visit the following site: ( This requires some add-o…
    Many developers have database experience, but are new to PostgreSQL. It has some truly inspiring capabilities. I have several years' experience with Microsoft's SQL Server. When I began working with MySQL, I wanted a quick-reference to MySQL (htt…
    Steps to create a PostgreSQL RDS instance in the Amazon cloud. We will cover some of the default settings and show how to connect to the instance once it is up and running.
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.

    733 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now