您好,登錄后才能下訂單哦!
create or replace function decode(variadic p_decode_list text[]) returns text as $$ declare -- 獲取數(shù)組長度(即入?yún)€數(shù)) v_len integer := array_length(p_decode_list, 1); -- 聲明存放返回值的變量 v_ret text; begin /* * 功能說明:模擬Oracle中的DECODE功能(字符串處理, 其它格式可以自行轉(zhuǎn)換返回值) * 參數(shù)說明:格式同Oracle相同,至少三個參數(shù) * 實(shí)現(xiàn)原理: 1、VARIADIC 允許變參; 2、Oracle中的DECODE是拿第一個數(shù)依次和之后的偶數(shù)位值進(jìn)行比較,相同則取偶數(shù)位+1的數(shù)值,否則取最后一位值(最后一位為偶數(shù)為,否則為null) */ -- 同Oracle相同當(dāng)參數(shù)不足三個拋出異常 if v_len >= 3 then -- Oracle中的DECODE是拿第一個數(shù)依次和之后的偶數(shù)位值進(jìn)行比較,相同則取偶數(shù)位+1的數(shù)值 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; -- 測試1 select decode('_a','_aa', 'x') a3, decode('_a','_aa', 'x', 's') a4, decode('_a', '_aa', 'x', '_b', 's') a5, decode('_b', '_aa', 'x', '_b', 's') a5, decode('_a', '_aa', 'x', '_b', 's', 'o', 'x', 'tt') a6; -- 測試2 with xx as ( select 'M' sex union all select 'F' sex union all select 'X' sex union all select 'Z' sex union all select '' sex ) select sex, decode(sex, 'M', '男', 'F', '女', decode(sex, 'X', '變性', '其它')) from xx;
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。