1. 程式人生 > >oracle函式翻譯字典項

oracle函式翻譯字典項

某一欄位的值為1,2,3,查詢的時候要把欄位翻譯成對應的漢字

需要建立3個函式,複製下面的3個函式到資料中。然後使用的時候把欄位傳入,呼叫函式FUNC_LICENCE_TYPE(attendUser)即可翻譯為漢字

函式1: 要翻譯的欄位 field_val


create or replace function FUNC_LICENCE_TYPE(field_val in varchar2)
return varchar2
is
  v_val varchar2(50);
  v_mult_val varchar2(100);
  res_back varchar2(100);
  cursor c_result is
  select t.column_value from table (SPLIT_FOR_ARRAY(field_val,',')) t;
  c_row c_result%rowtype;
begin
  for c_row in c_result loop
    select a.USER_NAME into v_val from SY_USER a where a.USER_ID = c_row.column_value;
    v_mult_val := v_mult_val|| ','||v_val;
  end loop;
  select substr(v_mult_val,2) into res_back from dual;
  return res_back;
  EXCEPTION
  WHEN OTHERS THEN
    RETURN '';
end FUNC_LICENCE_TYPE_RY;

函式2: 逗號分隔函式


CREATE OR REPLACE FUNCTION SPLIT_FOR_ARRAY(P_STR       IN VARCHAR2,
                                 P_DELIMITER IN VARCHAR2 DEFAULT (',') --分隔符,預設逗號
                                 )RETURN  array_container  IS
  J        INT := 0;
  I        INT := 1;
  LEN      INT := 0;
  LEN1     INT := 0;
  STR      VARCHAR2(4000);
  MY_SPLIT array_container := array_container();
BEGIN
  LEN  := LENGTH(P_STR);
  LEN1 := LENGTH(P_DELIMITER);

  WHILE J < LEN LOOP
    J := INSTR(P_STR, P_DELIMITER, I);

    IF J = 0 THEN
      J   := LEN;
      STR := SUBSTR(P_STR, I);
      MY_SPLIT.EXTEND;
      MY_SPLIT(MY_SPLIT.COUNT) := STR;

      IF I >= LEN THEN
        EXIT;
      END IF;
    ELSE
      STR := SUBSTR(P_STR, I, J - I);
      I   := J + LEN1;
      MY_SPLIT.EXTEND;
      MY_SPLIT(MY_SPLIT.COUNT) := STR;
    END IF;
  END LOOP;

  RETURN MY_SPLIT;
END SPLIT_FOR_ARRAY;

函式3: 定義型別函式
 

CREATE OR REPLACE TYPE "ARRAY_CONTAINER"  AS TABLE OF varchar2(100)