继承的父子关系的SQL属性

时间:2014-11-19 13:46:04

标签: sql sql-server sql-server-2008

您的意见将非常感谢.. !!请看看并建议..

两个表 -

表1

User(PK) | Permission_On
usr1     | Folder_A
usr2     | Folder_B
usr3     | Folder_C

表2

RowAddedTimeStamp(PK) | User(PK) | Substitute_User | BeginDate | EndDate
xxxx1                 | usr1     | usr1            | 1-1-2010  | 1-1-2010
xxxx2                 | usr2     | usr2            | 1-1-2010  | 1-1-2010
xxxx3                 | usr1     | usr2            | 1-1-2010  | 1-1-2012
xxxx4                 | usr3     | usr3            | 1-1-2010  | 1-1-2010
xxxx5                 | usr2     | usr3            | 1-1-2011  | 1-1-2013

我已经尝试过,但没有考虑替代用户如何获得特定用户的确切权限。

就像今天一样 -

2-2-2011 usr3 的权限为 - Folder_A,Folder_B,Folder_C

Folder_C  -- Direct permission for usr3
Folder_B  -- as usr3 is substitute of usr2 (between 1-1-2011  to 1-1-2013)
Folder_A  -- as usr2 is substitute of usr1 (between 1-1-2010  to 1-1-2012)

2-2-2012 usr3 的权限为 - Folder_B,Folder_C

Folder_C  -- Direct permission for usr3
Folder_B  -- as usr3 is substitute of usr2 (between 1-1-2011  to 1-1-2013)
Folder_A  -- **** NO because usr2 was substitute of usr1 (between 1-1-2010  to 1-1-2012) and today is 2-2-2012

2-2-2013 usr3 的权限为 - Folder_C

Folder_C  -- Direct permission for usr3
Folder_B  -- **** NO because usr3 was substitute of usr2 (between 1-1-2011  to 1-1-2013)  and today is 2-2-2013
Folder_A  -- **** NO because usr2 was substitute of usr1 (between 1-1-2010  to 1-1-2012) and today is 2-2-2013

1 个答案:

答案 0 :(得分:0)

使用MSSQL函数。与嵌套和递归查询相比,它将大大提高性能。

e.g。功能:

CREATE FUNCTION getUserList (@inputUser VARCHAR(250), @inputDate VARCHAR(250))
RETURNS @UserList TABLE
   (
    userName   varchar(250),
   )
AS
BEGIN
   INSERT @UserList
        SELECT a.Substitute_User,
        FROM Table2 AS a
        WHERE a.User = @inputUser
        AND a.BeginDate <= @inputDate
        AND a.EndDate >= @inputDate
   RETURN
END

然后将其用作替代用户列表的来源,例如

SELECT Permission_On FROM Table1 WHERE
User IN (SELECT userName FROM Table1('usr1','2-2-2011'))
OR User = 'usr1'

<强> - 编辑 -

哦,我明白了,我误解了替代用户的用户.. 在这种情况下,您可以使用SQL CTE功能:

看看这个 SQLFiddle

在此查询中,您应该替换用户以及查询中的两个日期

WITH AccessHierarchy ( Userr ) AS
(
   -- Base case
   SELECT
      Userr
   FROM Table1
   WHERE Userr = 'usr3'

   UNION ALL

   -- Recursive step
   SELECT
      e.Userr
   FROM Table2 e
      INNER JOIN AccessHierarchy eh ON
         e.Substitute_User = eh.Userr
      WHERE e.Userr != e.Substitute_User
      AND BeginDate <= '2011-05-05'
      AND EndDate >= '2011-05-05'
)

SELECT Permission_On FROM Table1
WHERE Userr IN
(Select Userr
FROM AccessHierarchy
)

您可以使用以下语句创建测试表:

CREATE TABLE Table1 
    (
     Userr VARCHAR(255) primary key, 
     Permission_On varchar(255)
    );

INSERT INTO Table1
(Userr, Permission_On)
VALUES
('usr1', 'folA'),
('usr2', 'folB'),
('usr3', 'folC');



CREATE TABLE Table2
    (
     Userr VARCHAR(255), 
     Substitute_User varchar(255), 
     BeginDate DATETIME, 
     EndDate DATETIME, 
    );

INSERT INTO Table2
(Userr, Substitute_User, BeginDate, EndDate)
VALUES
('usr1', 'usr1', '2010-01-01', '2010-01-01'),
('usr2', 'usr2', '2010-01-01', '2010-01-01'),
('usr1', 'usr2', '2010-01-01', '2012-01-01'),
('usr3', 'usr3', '2010-01-01', '2010-01-01'),
('usr2', 'usr3', '2011-01-01', '2013-01-01');