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

--
Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-sql

Reply via email to