Re: "and then" / "or else"

From: Michael Glaesemann <grzm(at)seespotcode(dot)net>
To: Christian Schröder <cs(at)deriva(dot)de>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: "and then" / "or else"
Date: 2007-11-17 13:20:16
Message-ID: 76AE4E3A-17DD-4D9E-861D-88C70F1A1A04@seespotcode.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general


On Nov 17, 2007, at 3:53 , Christian Schröder wrote:

> Unfortunately, the trick from the docs (chapter 4.2.12) using
> "case ... then" does not work inside an "if" statement (the "then"
> of the "case" is interpreted as belonging to the "if" and thus
> leads to a syntax error).

I think if you use parentheses you can avoid the syntax error:

CREATE FUNCTION test_case_in_if
(in in_a boolean, in in_b boolean, in in_c boolean)
RETURNS text
STABLE
STRICT
LANGUAGE plpgsql AS $body$
begin
if (CASE WHEN in_a then (in_b and in_c) else in_b end)
then
return 'first branch';
else
return 'second branch';
end if;
END
$body$;

test=# select test_case_in_if(true, true, true);
test_case_in_if
-----------------
first branch
(1 row)

test=# select test_case_in_if(true, false, true);
test_case_in_if
-----------------
second branch
(1 row)

test=# select test_case_in_if(false, true, false);
test_case_in_if
-----------------
first branch
(1 row)

Michael Glaesemann
grzm seespotcode net

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Michael Glaesemann 2007-11-17 13:24:51 Re: Composite types for composite primary/foreign keys?
Previous Message Christian Schröder 2007-11-17 08:53:40 "and then" / "or else"