From: | Craig Ringer <craig(at)postnewspapers(dot)com(dot)au> |
---|---|
To: | Denis Woodbury <denis(at)woodmic(dot)net> |
Cc: | pgsql-sql(at)postgresql(dot)org |
Subject: | Re: EXISTS |
Date: | 2008-10-14 07:48:33 |
Message-ID: | 48F44ED1.2010402@postnewspapers.com.au |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-sql |
Denis Woodbury wrote:
> Hi,
>
> I would like to know if this this type of statement can be used in
> Postgresql
>
> IF NOT EXISTS (SELECT 1 FROM Table WHERE col1 = 'mystring' )
> BEGIN
> ...
> END
PostgreSQL doesn't have any sort of free block flow control; it doesn't
have an IF statement or similar in SQL. You can usually just create a
PL/PgSQL function to do what you want.
It'd occasionally be nice to be able to write PL/PgSQL bodies in-line in
SQL code rather than explicitly creating then dropping a function when
you do need to do something a bit weird (usually in admin/maintenance
scripts) but the current approach does work fine.
It also helps that you can often achieve the required logic with plain,
standard SQL. The CASE statement is particularly useful:
SELECT
CASE
WHEN col1 = 'mystring' THEN [expression or function call]
END
FROM Table;
--
Craig Ringer
From | Date | Subject | |
---|---|---|---|
Next Message | Denis Woodbury | 2008-10-14 14:03:56 | Re: EXISTS |
Previous Message | Christopher Maier | 2008-10-13 20:12:20 | Re: Order of trigger execution: AFTER DELETE ON EACH ROW doesn't appear to be working properly |