Oracle queries whether the Department (326) of a person belongs to the Department string (329 | 2) or the sub-department of the Department string, 326329

Source: Internet
Author: User

Oracle queries whether the Department (326) of a person belongs to the Department string (329 | 2) or the sub-department of the Department string, 326329

 

Department 326 is the parent department of Department 329, and Department 2 and department 326 are departments at the same level;

Account muyunfei, belonging to department 329

The message is sent to Department 2 and department 326 (326 | 2). The objects of the sent departments are separated by "| ".

 

 

Query: whether the muyunfei account belongs to the Department string (329 | 2) or the sub-department of the Department string

1. First Use start ..... Connect by obtains the Department and parent department of the account muyunfei.

2. traverse the Department and parent department, and Judge in the nested loop body of the cyclic weight (this cycle is split with the department number separator "|, whether the two results are the same, the same, that is, the person belongs to the current department or its sub-departments, and 1 is returned to end the cycle. Otherwise, the cycle continues.

Create or replace function check_User_in_dept (to_party_id varchar2, -- department id, multiple use | split cur_user_id varchar2 -- personnel id) return varchar2 as/* function: determine whether the current person belongs to the current Department * 1 the current person belongs to this department, 0 does not belong to */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. party_id = (select B. department from wx_contacts B where B. user_id = cur_user_id) connect by. party_id = prior. parent_id; begin/* retrieve cursor data */for v_1 in c_1 loop/** split String Based on '|' and match data. If so, otherwise, the LOOP */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;/* reset cycle condition */v_length: = LENGTH (to_party_id); v_start: = 1; end loop; return '0'; end;


 

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.