Re: [SQL] bug with if ... then ... clause in views

From: Michael Glaesemann <grzm(at)myrealbox(dot)com>
To: Emil Rachovsky <zsevgymko(at)yahoo(dot)com>
Cc: "pgsql-sql(at)postgresql(dot)org" <pgsql-sql(at)postgresql(dot)org>, "pgsql-general(at)postgresql(dot)org" <pgsql-general(at)postgresql(dot)org>
Subject: Re: [SQL] bug with if ... then ... clause in views
Date: 2006-01-18 09:28:50
Message-ID: EFE37C81-1321-4820-919C-6367535BBD8E@myrealbox.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general pgsql-sql

On Jan 18, 2006, at 18:18 , Emil Rachovsky wrote:

> CREATE OR REPLACE VIEW public.SomeView
> as select d.id,
> if (true) then d.DocNumber endif from
> public.Z_Documents as d;
>
> I get the following error :
> syntax error at or near "then" at character 72

Well, one problem is that IF ... ENDIF is the correct syntax. You can
use it in PL/pgSQL, but that's a procedural language, and not what
you're doing here. You're probably looking for CASE, e.g.,

CREATE OR REPLACE VIEW public."SomeView" AS
SELECT d.id
, CASE WHEN TRUE
THEN d."DocNumber"
ELSE something_else
END as "DocNumber"
FROM public."Z_Documents" as d;

Note you need an ELSE clause: you can't have a variable number of
columns in the view (just like you can't have a variable number of
columns in a table). As an aside, you need to double-quote
identifiers if you want them to be case-sensitive: otherwise they'll
be down-cased.

Hope this helps.

Michael Glaesemann
grzm myrealbox com

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Joeseph Blowseph 2006-01-18 09:34:25 Re: Modify the password of the service account?
Previous Message Emil Rachovsky 2006-01-18 09:18:15 bug with if ... then ... clause in views

Browse pgsql-sql by date

  From Date Subject
Next Message Michael Glaesemann 2006-01-18 09:59:28 Re: Still struggling with history tables
Previous Message Emil Rachovsky 2006-01-18 09:18:15 bug with if ... then ... clause in views