Received: from localhost (unknown [200.46.208.211]) by mail.postgresql.org (Postfix) with ESMTP id 10B57634955 for ; Fri, 21 Aug 2009 15:17:36 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by localhost (mx1.hub.org [200.46.208.211]) (amavisd-maia, port 10024) with ESMTP id 86760-03 for ; Fri, 21 Aug 2009 18:17:22 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from mail.fmed.uba.ar (mail.fmed.uba.ar [157.92.152.1]) by mail.postgresql.org (Postfix) with ESMTP id 3DF7863421D for ; Fri, 21 Aug 2009 15:17:25 -0300 (ADT) Received: from [157.92.155.58] (mailbis [157.92.155.58]) by mail.fmed.uba.ar (Postfix) with ESMTP id 072CD39C201; Fri, 21 Aug 2009 15:16:24 -0300 (ART) Message-ID: <4A8EE3D5.3050501@fmed.uba.ar> Date: Fri, 21 Aug 2009 15:13:41 -0300 From: Gerardo Herzig User-Agent: Thunderbird 2.0.0.6 (X11/20070801) MIME-Version: 1.0 To: =?UTF-8?B?IlJvZHJpZ28gRS4gRGUgTGXDs24gUGxpY2V0Ig==?= CC: pgsql-sql@postgresql.org Subject: Re: using case to select 'which version to use' References: <4A8D6912.60207@fmed.uba.ar> In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-2.599 tagged_above=-10 required=5 tests=BAYES_00=-2.599 X-Spam-Level: X-Archive-Number: 200908/121 X-Sequence-Number: 33030 Rodrigo E. De León Plicet wrote: > On Thu, Aug 20, 2009 at 10:17 AM, Gerardo Herzig wrote: >> Hi all. I have 2 functions , which returns the same columns, but with >> tottaly different logics. I want to wrap it with a third function, who >> will receive a boolean argument to decide wich sub-function have to use. >> >> I want to use case, so i dont have to use pl/pgsql. >> >> This is a non-working example: >> >> >> CREATE OR REPLACE FUNCTION get_oferta(varchar, varchar, varchar, bool, >> OUT cod varchar , OUT description varchar) >> returns setof record >> security definer >> as >> $$ >> select * from case $4 when true then >> (select * from get_oferta_from_a($1, $2, $3)) >> else >> (select * from get_oferta_from_b($1, $2, $3)) >> end; >> $$ language sql; >> >> Can i use case for a case like this? >> >> Thanks! >> Gerardo > > Try: > > SELECT * > FROM get_oferta_from_a ($1, $2, $3) > WHERE $4 > UNION ALL > SELECT * > FROM get_oferta_from_b ($1, $2, $3) > WHERE NOT $4; > > Well, looks like it will work. I think i found the right syntax now: select foo.* from (select case $4 when false then get_oferta_from_a($1, $2, $3) else get_oferta_from_b($1, $2, $3) end) as foo; Thanks! Gerardo