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

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

From: Rory Campbell-Lange <rory(at)campbell-lange(dot)net>
To: Nathaniel Trellice <naptrel(at)yahoo(dot)co(dot)uk>
Cc: pgsql-novice(at)postgresql(dot)org
Subject: Re: Conditionally executing multiple statements in series as single SQL statement
Date: 2009-12-18 13:18:45
Message-ID: 20091218131845.GD22707@campbell-lange.net (view raw or flat)
Thread:
Lists: pgsql-novice
On 18/12/09, Nathaniel Trellice (naptrel(at)yahoo(dot)co(dot)uk) wrote:
> 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.

You could do something along the following lines:

CREATE OR REPLACE FUNCTION
    fn_test ( integer) RETURNS INTEGER
    AS $$
    DECLARE
        input    ALIAS   for $1;
        status   INTEGER := 0;
        returner INTEGER := 0;
    BEGIN
        PERFORM fn_test1 (input);
        IF NOT FOUND THEN
            RETURN returner;
        ELSE
           SELECT INTO status * FROM fn_test2 (input);
           IF status != 1 THEN
                RAISE NOTICE 'status from fn_test2 not expected %' % status
                RETURN returner;
           ELSE
                ...etc...
           END IF;
           returner = 1
           RETURN returner;
        END IF;
END;$$
    LANGUAGE plpgsql;

-
Rory Campbell-Lange
Director
rory(at)campbell-lange(dot)net

Campbell-Lange Workshop
www.campbell-lange.net
0207 6311 555
3 Tottenham Street London W1T 2AF
Registered in England No. 04551928

In response to

pgsql-novice by date

Next:From: Oliveiros C,Date: 2009-12-18 13:45:24
Subject: Re: Conditionally executing multiple statements in series as single SQL statement
Previous:From: Sean DavisDate: 2009-12-18 13:16:33
Subject: Re: Conditionally executing multiple statements in series as single SQL statement

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