oracle查詢某人所在的部門(326)是否屬於部門字串(329|2)或者該部門字串的子部門中,326329

來源:互聯網
上載者:User

oracle查詢某人所在的部門(326)是否屬於部門字串(329|2)或者該部門字串的子部門中,326329

 

部門326為部門329的父部門,部門2與部門326是同級部門;

賬戶muyunfei,屬於329部門

訊息發送給部門2及部門326兩個部門(326|2),發送的部門對象之間用“|”分割

 

 

查詢:muyunfei賬戶是否屬於該部門字串(329|2)或者該部門字串的子部門中

1、首先使用start with。。。。。connect by獲得賬戶muyunfei的部門及父部門

2、遍曆部門及父部門,在迴圈體重嵌套迴圈(該迴圈拆分以“|”為分隔字元的部門號),在嵌套的迴圈體中判斷,兩結果是否相同,相同,即人屬於當前部門或其子部門,返回1結束迴圈,否則繼續迴圈

create or replace function check_User_in_dept(to_party_id varchar2, --部門id,多個用|分割                                              cur_user_id varchar2 --人員id                                              ) return varchar2 as  /*  功能:判斷當前人是是否屬於當前部門中  * 1當前人屬於該部門,0不屬於  */  party_str varchar2(200);  v_length  NUMBER := LENGTH(to_party_id);  v_start   NUMBER := 1;  v_index   NUMBER;  cursor c_1 is    select *      from wx_party a     start with a.party_id = (select b.department                                from wx_contacts b                               where b.user_id = cur_user_id)    connect by a.party_id = prior a.parent_id;begin  /*檢索遊標資料*/  for v_1 in c_1 loop    /**根據‘|’拆分字串,並匹配資料,如果存在然後1,否則繼續迴圈*/    WHILE (v_start <= v_length) LOOP      v_index := INSTR(to_party_id, '|', v_start);       -- DBMS_OUTPUT.PUT_LINE('v_index:' || v_index);      IF v_index = 0 THEN        party_str := SUBSTR(to_party_id, v_start);        v_start   := v_length + 1;      ELSE        party_str := SUBSTR(to_party_id, v_start, v_index - v_start);        v_start   := v_index + 1;      END IF;      --DBMS_OUTPUT.PUT_LINE('party_str:' || party_str);      --DBMS_OUTPUT.PUT_LINE('party_id:' || v_1.party_id);      IF party_str = v_1.party_id then        return '1';      end if;    END LOOP;    /*重設迴圈條件*/    v_length := LENGTH(to_party_id);    v_start  := 1;  end loop;  return '0';end;


 

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.