Обсуждение: Porting Oracle Decode to Postgresql

Поиск
Список
Период
Сортировка

Porting Oracle Decode to Postgresql

От
MindTerm
Дата:
Dear all, I am writing to ask how to port oracle's decode( ..)
function to postgresql ? 
 It seems that plpgsql is not a possible way because
there are undefined input parameters for decode()
function. How about c ? any suggestion ? any existing
source ?

M.T.

__________________________________________________
Do You Yahoo!?
Yahoo! GeoCities - quick and easy web site hosting, just $8.95/month.
http://geocities.yahoo.com/ps/info1


Re: Porting Oracle Decode to Postgresql

От
Tod McQuillin
Дата:
On Thu, 22 Nov 2001, MindTerm wrote:

>   I am writing to ask how to port oracle's decode( ..)
> function to postgresql ?

The ANSI SQL version of decode is CASE.

See http://www.postgresql.org/idocs/index.php?functions-conditional.html
for the full documentation, but basically, it works like this:

Oracle:
decode (value, 0, 'zero', 1, 'one', 'unknown')

ANSI:
CASE WHEN value=0 THEN 'zero' WHEN value=1 THEN 'one' ELSE 'unknown' END

or

CASE value WHEN 0 THEN 'zero' WHEN 1 THEN 'one' ELSE 'unknown' END

-- 
Tod McQuillin




Re: Porting Oracle Decode to Postgresql

От
MindTerm
Дата:
Dear Tod McQuillin,
  Can perform the action as same as oracle in
postgresql ? 
  e.g.  Select decode_function( ... , ... , ..... ) from ..

  I can write a plpgsql to simulate the nvl function
by using coalesce .

CREATE FUNCTION nvl( numeric, numeric ) RETURNS
varchar AS '
DECLARE  input_value  ALIAS FOR $1;  else_value   ALIAS FOR $2;  output_value numeric;
BEGIN  select coalesce( input_value, else_value ) into
output_value ;  return output_value ;
END;   
' LANGUAGE 'plpgsql' ;

M.T.


--- Tod McQuillin <devin@spamcop.net> wrote:
> On Thu, 22 Nov 2001, MindTerm wrote:
> 
> >   I am writing to ask how to port oracle's decode(
> ..)
> > function to postgresql ?
> 
> The ANSI SQL version of decode is CASE.
> 
> See
>
http://www.postgresql.org/idocs/index.php?functions-conditional.html
> for the full documentation, but basically, it works
> like this:
> 
> Oracle:
> decode (value, 0, 'zero', 1, 'one', 'unknown')
> 
> ANSI:
> CASE WHEN value=0 THEN 'zero' WHEN value=1 THEN
> 'one' ELSE 'unknown' END
> 
> or
> 
> CASE value WHEN 0 THEN 'zero' WHEN 1 THEN 'one' ELSE
> 'unknown' END
> 
> -- 
> Tod McQuillin
> 
> 


__________________________________________________
Do You Yahoo!?
Yahoo! GeoCities - quick and easy web site hosting, just $8.95/month.
http://geocities.yahoo.com/ps/info1


Re: Porting Oracle Decode to Postgresql

От
MindTerm
Дата:
Dear Michael Ansley ,
 It seems that c program in postgresql support
unspecified number of input parameter. The example is
as following :
 But in the CREATE FUNCTION , it needs to specify the
number and type of arguments... how to rewrite it to
support vary number of input parameters ?


c program:

PG_FUNCTION_INFO_V1(c_overpaid);

Datum
c_overpaid(PG_FUNCTION_ARGS)
{   TupleTableSlot  *t = (TupleTableSlot *)
PG_GETARG_POINTER(0);   int32            limit = PG_GETARG_INT32(1);   bool isnull;   int32 salary;
   salary = DatumGetInt32(GetAttributeByName(t,
"salary", &isnull));   if (isnull)       PG_RETURN_BOOL(false);   /* Alternatively, we might prefer to do
PG_RETURN_NULL() for null salary */
   PG_RETURN_BOOL(salary > limit);
}


--- "Michael Ansley (UK)"
<Michael.Ansley@intec-telecom-systems.com> wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
> 
> Well, because you can overload functions, I guess
> that you could
> create a new decode for each number of parameters up
> to a limit.  Is
> it likely that your program uses more than about
> eight or nine
> parameters?  Then create the three or four overloads
> that it needs to
> get that many, and if you need more, then just
> create them when you
> do.
> 
> Cheers...
> 
> 
> MikeA



__________________________________________________
Do You Yahoo!?
Yahoo! GeoCities - quick and easy web site hosting, just $8.95/month.
http://geocities.yahoo.com/ps/info1