Solved

PostgreSQL conditions

Posted on 2000-03-21
1
1,610 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
one-way data "masking" MD5 sql 26 146
Linking dictionary and columns together 2 62
query returning everything 11 91
Partially Contained Databases - Cross Database Queries 3 44
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!
Read about achieving the basic levels of HRIS security in the workplace.
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…

920 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

15 Experts available now in Live!

Get 1:1 Help Now