Solved

PostgreSQL conditions

Posted on 2000-03-21
1
1,609 Views
Last Modified: 2012-06-21
Hi!

How do I check some conditions in PostgreSQL ?!
I couldn't find any "IF" or "CASE" commands in the help.

All I want is something like that:
IF 1=1 SELECT 'True' ELSE SELECT 'False'

and more advanced:
how do I check if a table exists?
I know it's stored in pg_class table and I can check the relname field, but how can I perform something after that ?
Let's say I want to drop the table if it exists...
In Microsoft SQL server I could write somthing like that:
IF EXISTS (SELECT the table name from a system tables)
  DROP the table

How can it be done with PostgreSQL??

Thanks.
0
Comment
Question by:alex_b
1 Comment
 

Accepted Solution

by:
xbrent earned 100 total points
ID: 4380612
1>
> How do I check some conditions in PostgreSQL ?!

PostgreSQL provides for this in the form of "pl/pgsql". It is a procedural language for postgres.  See the docs (http://www.postgresql.org/users-lounge/docs/7.0/postgres/c4091.htm) for more.

IF var_i < 1 THEN
  SELECT true_column FROM table;
ELSE
  SELECT false_column FROM table;
END IF;

2>
>how do I check if a table exists..then drop?

You're correct - you check for the table using plain SQL with some plpgsql logic:

IF (SELECT tablename
      FROM pg_tables
     WHERE tablename = 'checking_for_table') ISNULL THEN
 DROP TABLE checking_for_table;
END IF;

(LANGUAGE 'plpgsql';)
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Shadow IT is coming out of the shadows as more businesses are choosing cloud-based applications. It is now a multi-cloud world for most organizations. Simultaneously, most businesses have yet to consolidate with one cloud provider or define an offic…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

760 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

23 Experts available now in Live!

Get 1:1 Help Now