select a.code,a.cType.a.其他字段,a.cDeptId from tbl_report a where 1=1 and ( 如果 a.cType=1 那么允许当前部门及其以下用户查看 如果 a.cType=2 那么允许当前部门及其以上用户查看 )入参:用户ID、点击的部门节点
在where中不能直接使用select的列进行case或者decode, 但是楼主可以这样想啊 select a.code,a.cType.a.其他字段,a.cDeptId from tbl_report a where 1=1 and ( a.cType=1 那么允许当前部门及其以下用户查看 ) union all select a.code,a.cType.a.其他字段,a.cDeptId from tbl_report a where 1=1 and ( 如果 a.cType=2 那么允许当前部门及其以上用户查看 ) 转换下思路呗!
其实现在就是这样做的,由于还有其他的一些逻辑,所以导致了查询SQL过于庞大。所以想问问大家。 我用了另一个方法,就是写一个 Functions f_assign_dept(cCurrentDeptId in varchar2,cDeptId in varchar2, cType in char) 方法中用到了oracle 向上递归及向下递归部门树。create or replace function f_assign_dept(cCurrentDeptId in varchar2,cDeptId in varchar2, cType in char) return integer is n_Result integer; begin if cType='1' then select count(*) into n_Result from tbl_dept a where orgid=cCurrentDeptId and rownum=1 start with a.orgid = cDeptId connect by prior a.parentid = a.orgid; elsif cType='0' then select count(*) into n_Result from tbl_dept a where orgid=cCurrentDeptId and rownum=1 start with a.orgid = cDeptId connect by prior a.orgid = a.parentid; else select 0 into n_Result from dual; end if; return(n_Result); end f_assign_dept; 前台查询 select a.code,a.cDeptId,a.cType from tbl_report a where 1=1 and f_assign_dept(入参部门ID,a.cDeptId,a.cType)>0 但感觉速度很慢。
code(编码) # cType(类型)# cDeptId(部门编码)# cRe
---------------------------------------------------------------
10001 # 1 # A2 # 部门A2及A21部门下的用户可以查看(当前部门及其以下)
10002 # 2 # B1 # 部门B1及B可以查看(当前部门及其以上)
a.code,a.cType.a.其他字段,a.cDeptId
from tbl_report a
where 1=1
and (
如果 a.cType=1 那么允许当前部门及其以下用户查看
如果 a.cType=2 那么允许当前部门及其以上用户查看
)入参:用户ID、点击的部门节点
但是楼主可以这样想啊
select
a.code,a.cType.a.其他字段,a.cDeptId
from tbl_report a
where 1=1
and (
a.cType=1 那么允许当前部门及其以下用户查看
)
union all
select
a.code,a.cType.a.其他字段,a.cDeptId
from tbl_report a
where 1=1
and (
如果 a.cType=2 那么允许当前部门及其以上用户查看
)
转换下思路呗!
我用了另一个方法,就是写一个 Functions
f_assign_dept(cCurrentDeptId in varchar2,cDeptId in varchar2, cType in char)
方法中用到了oracle 向上递归及向下递归部门树。create or replace function f_assign_dept(cCurrentDeptId in varchar2,cDeptId in varchar2, cType in char) return integer is
n_Result integer;
begin
if cType='1' then
select count(*) into n_Result from tbl_dept a where orgid=cCurrentDeptId and rownum=1 start with a.orgid = cDeptId connect by prior a.parentid = a.orgid;
elsif cType='0' then
select count(*) into n_Result from tbl_dept a where orgid=cCurrentDeptId and rownum=1 start with a.orgid = cDeptId connect by prior a.orgid = a.parentid;
else
select 0 into n_Result from dual;
end if;
return(n_Result);
end f_assign_dept;
前台查询
select a.code,a.cDeptId,a.cType from tbl_report a where 1=1
and f_assign_dept(入参部门ID,a.cDeptId,a.cType)>0
但感觉速度很慢。