sql视图与其他表

时间:2018-05-16 12:26:05

标签: sql sql-server sql-server-2012

给出表Customers,例如:

CustomerID  FirstName   MiddleInitial   LastName
64          Abby        A               Garcia
65          Abby        C               Mehta
66          Abby        E               Chandra
67          Abby        J               Kapoor
68          Abby        J               Sanchez
69          Abby        K               Kovár
70          Abby        L               Sai
71          Abby        M               Lopez
72          Abby        P               Gonzalez
73          Abby        P               Rana

我想根据另一个表中的定义限制对(table,column,row)的访问,让我们说m_Customers定义如下:

MetadataID  Field           Type        Access
1           Customers       table       "Group1","Group2","Group3"
2           MiddleInitial   column      "Group1","Group3"
3           18              row         "Group1","Group3" 

基于此,如何制定一个查询,以确保如果:

    来自* Group3的
  • 您无法看到列MiddleInitials并使用CustomerId = 18
  • 来自“Group4”的
  • 您无法在Customers表中看到数据

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

您的排除和包含似乎是混合的。您将Group3包含在Customers列和第18行中,但Group 4不包含在表Customers中。使您的m_Customers表仅包含具有访问权限的表或仅包含受限制的表 ​​- 而不是两者。

通常更快更容易管理更改m_Customers的数据结构,以便为每个组使用记录。

MetadataID  Field           Type    Access
1           Customers       table   Group1
2           Customers       table   Group2
3           Customers       table   Group3
4           MiddleInitial   column  Group1
5           MiddleInitial   column  Group3
6           18              row     Group1
7           18              row     Group3

以下是两个选项;你可以检查一下表现。

SELECT MAX(col.customerID) AS customerID, MAX(col.FirstName) AS FirstName, MAX(col.MiddleInitial) AS MiddleIntitial, MAX(col.LastName) AS LastName
FROM Customers AS c
INNER JOIN m_Customers AS mRow ON mRow.[Type] = 'row' AND mRow.CustomerID = c.Field AND mRow.Access = @group
LEFT JOIN (
    SELECT
        IIF(mCol.Field = 'customerID', c.customerID, NULL) AS customerID,
        IIF(mCol.Field = 'Firstname', c.FirstName, NULL) AS FirstName,
        IIF(mCol.Field = 'MiddleInitial', c.FirMiddleInitialstName, NULL) AS MiddleInitial,
        IIF(mCol.Field = 'LastName', c.LastName, NULL) AS LastName
    FROM Customers AS c
    INNER JOIN  m_Customers AS mCol ON mCol.[Type] = 'column' AND mCol.Field = 'MiddleInitial' AND mCol.Access = @group
    ) AS col ON col.customerID = c.CustomerID
GROUP BY col.customerID, col.FirstName, col.MiddleInitial, col.LastName

SELECT customerID.customerID, FirstName.FirstName, MiddleInitial.MiddleInitial, LastName.LastName
FROM Customers AS c
INNER JOIN m_Customers AS mRow ON mRow.[Type] = 'row' AND mRow.CustomerID = c.Field AND mRow.Access = @group
LEFT JOIN (
    SELECT c.customerID
    FROM Customers AS c
    INNER JOIN  m_Customers AS mCol ON mCol.[Type] = 'column' AND mCol.Field = 'customerID' AND mCol.Access = @group
    ) AS customerID ON customerID.customerID = c.customerID
LEFT JOIN (
    SELECT c.customerID, c.FirstName
    FROM Customers AS c
    INNER JOIN  m_Customers AS mCol ON mCol.[Type] = 'column' AND mCol.Field = 'FirstName' AND mCol.Access = @group
    ) AS FirstName ON FirstName.customerID = c.customerID
LEFT JOIN (
    SELECT c.customerID, c.MiddleInitial
    FROM Customers AS c
    INNER JOIN  m_Customers AS mCol ON mCol.[Type] = 'column' AND mCol.Field = 'MiddleInitial' AND mCol.Access = @group
    ) AS MiddleInitial ON MiddleInitial.customerID = c.customerID
LEFT JOIN (
    SELECT c.customerID, c.LastName
    FROM Customers AS c
    INNER JOIN  m_Customers AS mCol ON mCol.[Type] = 'column' AND mCol.Field = 'LastName' AND mCol.Access = @group
    ) AS LastName ON LastName.customerID = c.customerID

考虑将列的名称从Type更改为非保留字。