可以在SQL Server层次型方法IsDescendantOf接受多个输入值?(Can SQL S

2019-09-17 01:01发布

我使用的位置存储hierarchyid数据类型。 用户可以通过位置(限制LocationId )。 如果用户具有超过1只的位置限制在hierarchyid数据类型的IsDescendantOf方法必须与一个再次调用OR

实施例(由LocationId 5和6滤波雇员):

SELECT * FROM Employee
INNER JOIN Location ON Employee.LocationId = Location.LocationId
WHERE Location.Node.IsDescendantOf((SELECT TOP 1 Node
    FROM Location 
    WHERE LocationId = 5)) = 1
OR 
Location.Node.IsDescendantOf((SELECT TOP 1 Node
    FROM Location 
    WHERE LocationId=6)) = 1`

这工作正常2个LocationId过滤器,但如果这种增长和一个人有说10层的过滤器。 IsDescendantOf可以工作,如SQL IN子句?

表中:

CREATE TABLE Location (
LocationId int NOT NULL PRIMARY KEY IDENTITY(1,1),
Name       nvarchar(100) NOT NULL,
[Node]  hierarchyid    NOT NULL,
[ParentNode]  AS ([Node].[GetAncestor]((1))) PERSISTED,
[Level]  AS ([Node].[GetLevel]()) PERSISTED,
);
CREATE TABLE [dbo].[Employee] (
[EmployeeId] [int] PRIMARY KEY IDENTITY(1,1) NOT NULL,
[LocationId] [int] NULL,
[Name] [nvarchar](50) NULL
) ;

Answer 1:

:我就将此第二溶液(点6)。

1)你可以使用一个表变量来存储所有的搜索位置(例如: DECLARE @SearchedAncestorLocation TABLE(LocationId INT PRIMARY KEY)

2)你必须找到HIERARCHYID's节点从每个位置ID @SearchedAncestorLocation

3)你要做的INNER JOIN使用此过滤员工的位置: employee_location.Node.IsDescendantOf(searched_location.Node) = 1

4)我想你应该添加一个UNIQUE(Node)约束Location表,以防止重复的位置(复制节点)。

5) 解决方案一 : 演示这里 。

DECLARE @Location TABLE(
LocationId  int NOT NULL PRIMARY KEY,
Name        nvarchar(100) NOT NULL,
[Node]      hierarchyid    NOT NULL,
UNIQUE ([Node])
);

DECLARE @Employee TABLE (
[EmployeeId] [int] PRIMARY KEY,
[LocationId] [int] NULL,
[Name] [nvarchar](50) NULL
);

INSERT  @Location(LocationId, Name, [Node])
VALUES  ( 1, N'A',     '/1/'),
        ( 2, N'AA',    '/1/1/'),
        ( 3, N'AA-1',  '/1/1/1/'), -- <-- First employee  @ AA-1
        ( 4, N'AA-2',  '/1/1/2/'),
        ( 5, N'AA-3',  '/1/1/3/'),
        ( 6, N'AB',    '/1/2/'),
        ( 7, N'AA-1',  '/1/2/1/'),
        ( 8, N'AB-2',  '/1/2/2/'),

        ( 9, N'B',     '/2/'),
        (10, N'BA',    '/2/1/'),
        (11, N'BA-1',  '/2/1/1/'), -- <-- Second employee @ BA-1
        (12, N'BA-2',  '/2/1/2/'),
        (13, N'BA-3',  '/2/1/3/'),
        (14, N'BB',    '/2/2/'),
        (15, N'BB-1',  '/2/2/1/');

INSERT  @Employee(EmployeeId, [Name], LocationId)
VALUES  (1,  N'Ion Ionescu',   3), -- AA-1
        (2, N'Geo Georgescu', 11); -- BA-1

DECLARE @SearchedAncestorLocation TABLE(LocationId INT PRIMARY KEY);
INSERT  @SearchedAncestorLocation 
VALUES  (1), --A 
        (2), --AA
        (3), --AA-1
        (9), --B
       (10), --BA
       (14); --BB

SELECT  e.*, 
        el.Name             AS EmpLocationName,
        el.Node.ToString()  AS EmpLocationHID,
        s.LocationId        AS SearchedLocationId,
        sl.Name             AS SearchedLocationName,
        sl.Node.ToString()  AS SearchedLocationHID
FROM    @Employee e
INNER JOIN  @Location el ON e.LocationId = el.LocationId
INNER JOIN  @Location sl ON el.Node.IsDescendantOf(sl.Node) = 1
INNER JOIN  @SearchedAncestorLocation s ON sl.LocationId = s.LocationId 
--AND           sl.Node <> el.Node

结果:

EmployeeId LocationId  Name          EmpLocationName EmpLocationHID SearchedLocationId SearchedLocationName SearchedLocationHID
---------- ----------- ------------- --------------- -------------- ------------------ -------------------- -------------------
1          3           Ion Ionescu   AA-1            /1/1/1/        1                  A                    /1/
1          3           Ion Ionescu   AA-1            /1/1/1/        2                  AA                   /1/1/
1          3           Ion Ionescu   AA-1            /1/1/1/        3                  AA-1                 /1/1/1/
2          11          Geo Georgescu BA-1            /2/1/1/        9                  B                    /2/
2          11          Geo Georgescu BA-1            /2/1/1/        10                 BA                   /2/1/

结果,如果你取消注释最后一行( AND sl.Node <> el.Node ):

EmployeeId LocationId  Name          EmpLocationName EmpLocationHID SearchedLocationId SearchedLocationName SearchedLocationHID
---------- ----------- ------------- --------------- -------------- ------------------ -------------------- -------------------
1          3           Ion Ionescu   AA-1            /1/1/1/        1                  A                    /1/
1          3           Ion Ionescu   AA-1            /1/1/1/        2                  AA                   /1/1/
2          11          Geo Georgescu BA-1            /2/1/1/        9                  B                    /2/
2          11          Geo Georgescu BA-1            /2/1/1/        10                 BA                   /2/1/

6) 第二溶液

SELECT  e.EmployeeId,
        e.LocationId,
        e.Name
FROM    @Employee e
INNER JOIN  @Location el ON e.LocationId = el.LocationId
WHERE EXISTS 
(
        SELECT  *
        FROM    @SearchedAncestorLocation s
        INNER JOIN @Location sl ON s.LocationId = sl.LocationId 
        WHERE   el.Node.IsDescendantOf(sl.Node) = 1
        --AND       el.Node <> sl.Node 
);


文章来源: Can SQL Server Hierarchy type method IsDescendantOf accept multiple input values?