[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2105
  • Last Modified:

pgAdmin III query tool "IF" statement ERROR

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
ELSE
      SELECT 1
END IF;

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!!!
0
FreightTrain
Asked:
FreightTrain
  • 2
1 Solution
 
earth man2Commented:
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;
 case
------
    2
(1 row)

select case when EXISTS(SELECT relname FROM pg_class WHERE relname = 'Angelina_VR_Data') THEN 2 else 1 end;
1
 
FreightTrainAuthor Commented:
Two Words: YOU ROCK!!!  Thanks again earthman2!!!  This is so easy, once you know!!!
0
 
Vishakha TannaCommented:
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
0
 
earth man2Commented:
Vishakna Tanna, you need to open a new question rather than tag yours onto the end of another.
0

Featured Post

Get quick recovery of individual SharePoint items

Free tool – Veeam Explorer for Microsoft SharePoint, enables fast, easy restores of SharePoint sites, documents, libraries and lists — all with no agents to manage and no additional licenses to buy.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now