23. 根据公司查找底下的所有部门,根据部门找对应的公司

/**

*   公司下存在子公司,子公司下存在部门,部门下存在子部门

*   公司下存在部门,部门下存在子部门

*   根据公司找底下的所有部门:select * from lbOrganization where fun_findComOrgId(ID)=公司ID;

*   根据部门对应的公司:select fun_findComOrgId(部门ID) from dual;

*/

create or replace function fun_findComOrgId(--查询所属公司或集团的id
p_orgid int--传人组织机构id
)
return int is

v_flag int:=0;--所属公司或集团的id
v_orgtype int;--组织机构类型变量,2为公司,3为部门
v_orgid int;--存放输入的组织机构id,或查出的

begin
v_orgid:=p_orgid;

while v_flag=0 loop
select orgtype into v_orgtype from lborganization where id=v_orgid;
if v_orgtype<=2 then
v_flag:=v_orgid;--v_flag=所属公司id
else
v_flag:=0;
end if;
select fid into v_orgid from lborganization where id=v_orgid;

end loop;

return (v_flag);

end;
/

原文地址:https://www.cnblogs.com/zkx4213/p/5012265.html