日期:2014-05-17 浏览次数:20477 次
select * from
(
select * from
(
select * from tlpdmd where tlpdmd01='role001'
and tdma03
not in(select tdma03 from tlpdmd where tdma03 not in(select tdma01 from tlpdmd) and tdma03<>'root')
) x
where x.tdma03
in
(
select tdma01 from tlpdmd where tlpdmd01='role001'
and tdma03
not in(select tdma03 from tlpdmd where tdma03 not in(select tdma01 from tlpdmd) and tdma03<>'root')
) or x.tdma03='root'
)y
where y.tdma03
in
(
select tdma01 from
(
select * from tlpdmd where tlpdmd01='role001'
and tdma03
not in(select tdma03 from tlpdmd where tdma03 not in(select tdma01 from tlpdmd) and tdma03<>'root')
) x
where x.tdma03
in
(
select tdma01 from tlpdmd where tlpdmd01='role001'
and tdma03
not in(select tdma03 from tlpdmd where tdma03 not in(select tdma01 from tlpdmd) and tdma03<>'root')
) or x.tdma03='root'
) or y.tdma03='root'
USE tempdb
GO
-- 建立演示环境
CREATE TABLE Dept(
id int PRIMARY KEY,
parent_id int,
name nvarchar(20))
INSERT Dept
SELECT 0, 0, N'<全部>' UNION ALL
SELECT 1, 0, N'财务部' UNION ALL
SELECT 2, 0, N'行政部' UNION ALL
SELECT 3, 0, N'业务部' UNION ALL
SELECT 4, 0, N'业务部' UNION ALL
SELECT 5, 4, N'销售部' UNION ALL
SELECT 6, 4, N'MIS' UNION ALL
SELECT 7, 6, N'UI' UNION ALL
SELECT 8, 6, N'软件开发' UNION ALL
SELECT 9, 8, N'内部开发'
GO
-- 查询指定部门下面的所有部门
DECLARE @Dept_name nvarchar(20)
SET @Dept_name = N'MIS'
;WITH
DEPTS AS(
-- 定位点成员
SELECT * FROM Dept
WHERE name = @Dept_name
UNION