From: Oisin Glynn <me@oisinglynn.com>
To: Daniel CAUNE <d.caune@free.fr>
Cc: pgsql-sql@postgresql.org
Subject: Re: Evaluation of if conditions
Date: Wed, 06 Sep 2006 19:04:36 -0400
Message-ID: <44FF5404.2010604@oisinglynn.com> (raw)
In-Reply-To: <001601c6d204$5d2cd840$0b00a8c0@tedy>
References: <001601c6d204$5d2cd840$0b00a8c0@tedy>
Daniel CAUNE wrote:
> Hi,
>
> How does the IF statement evaluate conditions? Does it evaluate conditions
> following their declaration order from left to right? In case of
> or-conditions, does the IF statement stop evaluating conditions whenever a
> first or-condition is true?
>
> The following snippet seems to be invalid, which let me think that PL/PGSQL
> evaluates all the conditions:
>
> IF (TG_OP = 'INSERT') OR
> (OLD.bar = ...) THEN
> statement
> END IF;
>
> Should be rewritten as (for example):
>
> IF (TG_OP = 'INSERT') THEN
> statement
> ELSIF (OLD.bar = ...) THEN
> statement
> END IF;
>
>
> Regards,
>
> --
> Daniel
>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 9: In versions below 8.0, the planner will ignore your desire to
> choose an index scan if your joining column's datatypes do not
> match
>
The following is working fine for me on 8.1.x on Windows. I am not sure
what order it is evaluating the if statement in but it is working correctly.
Oisin
CREATE OR REPLACE FUNCTION zfunc_testor(bool, bool)
RETURNS "varchar" AS
$BODY$DECLARE
v_1 boolean;
v_2 boolean;
BEGIN
v_1 :=$1;
v_2 := $2;
if (v_1 = TRUE) OR (v_2 = TRUE) then
return 'At least 1 true';
else
return 'neither true';
end if;
END;$BODY$
LANGUAGE 'plpgsql' VOLATILE;
ALTER FUNCTION zfunc_testor(bool, bool) OWNER TO postgres;
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: pgsql-sql@postgresql.org
Cc: me@oisinglynn.com, d.caune@free.fr
Subject: Re: Evaluation of if conditions
In-Reply-To: <44FF5404.2010604@oisinglynn.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox