Skip site navigation (1) Skip section navigation (2)

Re: Conditionally executing multiple statements in series as single SQL statement

From: Mladen Gogala <mladen(dot)gogala(at)vmsinfo(dot)com>
To: pgsql-novice(at)postgresql(dot)org
Subject: Re: Conditionally executing multiple statements in series as single SQL statement
Date: 2009-12-20 06:57:03
Message-ID: 1261292223.27870.8.camel@nyclapwxp2622 (view raw, whole thread or download thread mbox)
Lists: 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 

The Leader in Integrated Media
Intelligence Solutions

In response to

pgsql-novice by date

Next:From: Damian CareyDate: 2009-12-22 06:50:24
Subject: SQL Syntax for ordering a sequence
Previous:From: listsDate: 2009-12-19 15:42:42
Subject: How to use a db in UTF-8 encoding under Windows

Privacy Policy | About PostgreSQL
Copyright © 1996-2017 The PostgreSQL Global Development Group