?
Solved

PostgreSQL conditions

Posted on 2000-03-21
1
Medium Priority
?
1,617 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 

Accepted Solution

by:
xbrent earned 200 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Your data is at risk. Probably more today that at any other time in history. There are simply more people with more access to the Web with bad intentions.
Backups and Disaster RecoveryIn this post, we’ll look at strategies for backups and disaster recovery.
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…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

770 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