Re: Conditionally executing multiple statements in series as single SQL statement - Mailing list pgsql-novice

From Mladen Gogala
Subject Re: Conditionally executing multiple statements in series as single SQL statement
Date
Msg-id 1261292223.27870.8.camel@nyclapwxp2622
Whole thread Raw
In response to Conditionally executing multiple statements in series as single SQL statement  (Nathaniel Trellice <naptrel@yahoo.co.uk>)
List pgsql-novice
Nathaniel, PostgreSQL does support Perl which is full of tricks. I dislike such tricks because they make the code much harder to read. For that reason even in Perl I sometimes write

if (!defined($a)) then { $a="value"; }

instead just

$a ||= "value";

The latter well known Perl idiom is much shorter and much easier to type but much harder to read, especially for people not proficient in Perl. The same applies to the "&&" trick which will execute the second part only if the first part has executed correctly.

On Fri, 2009-12-18 at 13:01 +0000, Nathaniel Trellice wrote:
Hi all,

In C, and many other programming languages, statements like the following are popular:

int status = (do_first_thing() && do_second_thing() && do_third_thing() && do_fourth_thing());

With this kind of expression, the program calls the function 'do_first_thing'. If, and only if, that returns non-zero, 'do_second_thing' will be executed. Again, if and only if that returns non-zero, 'do_third_thing' is executed. Etc.

In other words, later statements will only be executed if all before them have 'gone well'. When a statement 'fails', no further expressions are executed.. The variable 'status' is non-zero if, and only if, all four things were successfully executed.

For convenience, I'd really like to be able to achieve similar behaviour within an SQL statement, i.e. present multiple statements (such as INSERT statements) and only execute the later ones if the earlier ones have been executed without error. And I'd like to be able to present all the statements within a single, compound SQL statement to the database.

Is such a thing possible, using any fancy SQL syntactic tricks?


Nathaniel






Mladen Gogala
Sr. Oracle DBA
1500 Broadway
New York, NY 10036
(212) 329-5251
www.vmsinfo.com



The Leader in Integrated Media Intelligence Solutions



Attachment

pgsql-novice by date:

Previous
From: lists@kambanaria.org
Date:
Subject: How to use a db in UTF-8 encoding under Windows
Next
From: Damian Carey
Date:
Subject: SQL Syntax for ordering a sequence