Solved

PostgreSQL conditions

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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Exchange 2010 Mailbox Database offline 2 123
SQL Database connect change in Access 2015 5 59
How to load 2 images in same column in Delphi 2 47
ORA-02288: invalid OPEN mode 2 31
This article describes some very basic things about SQL Server filegroups.
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
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…

831 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