oracle中有一个decodde函数,开发同学都很喜欢。从Oracle迁移到GP时,如果有decode ,代码量修改会变少
以下为GP的decode实现
create or replace function pg_catalog.decode(variadic p_decode_list text[])
returns text as
$$
declare
v_len integer := array_length(p_decode_list, 1);
v_ret text;
begin
if v_len >= 3 then
for i in 2..(v_len - 1) loop
v_ret := null;
if mod(i, 2) = 0 then
if p_decode_list[1] = p_decode_list[i] then
v_ret := p_decode_list[i+1];
elsif p_decode_list[1] <> p_decode_list[i] then
if v_len = i + 2 and v_len > 3 then
v_ret := p_decode_list[v_len];
end if;
end if;
end if;
exit when v_ret is not null;
end loop;
else
raise exception 'UPG-00938: not enough args for function.';
end if;
return v_ret;
end;
$$
language plpgsql;