I need to port from Oracle a stored procedure that uses decode()
extensively. That is, I can't use series of CASE WHEN expr THEN expr [...] ELSE
as the guide suggests.
I wanted to create a variadic function, but here's the problem: in Oracle the function can accept any number of key-value pairs and the type of the key not necessarily matches the type of the value:
select decode(0 ,0,'a' ,1,'b' ,2,'c' /*,...*/ ,'dflt') from dual;
I tried to use anyarray
:
create or replace function decode(VARIADIC args anyarray) RETURNS text AS $$
SELECT null::text;
$$ LANGUAGE SQL;
But this only works when all the arguments are of the same type:
select decode(0,0,0); -- ok
select decode('x'::text,'x'::text,'x'::text); -- ok
select decode(0,0,'a'::text); -- No function matches the given name and argument types
If the desired syntax is not possible please advise another way of passing the expr
, the set of pairs, and the default value while keeping their positions same as in Oracle.