多对多表的递归CTE

时间:2018-10-22 20:11:31

标签: sql-server tsql common-table-expression recursive-cte

我正在使用递归CTE来使用外键跟踪数据库中的所有关系,以便创建一些测试数据。一切运行良好,直到我找到一个包含多对多关系的表为止。我已经看过与多对多表相关的其他一些答案,但这只是不随便点击-有人可以看看我拥有什么,并帮助我指出正确的方向吗?具体来说,发生的事情是我从客户端访问了一个表,该表是“预算/客户”表(id,budgetid,clientid)。此时,递归停止;我希望/期望它遍历预算表(通过budgetid)并在此之后继续访问任何相关表。

|Users      |      |Client       |       |BudgetClient |      |Budget       |
|userid (pk)|==\   |clientid (pk)|==\    |id (pk)      |   /==|budgetid (pk)|
                \==|userid (fk)  |   \   |budgetid (fk)|==/
                                      \==|clientid (fk)|

现有代码(当前尝试)如下:

    WITH cte AS
    (
        SELECT DISTINCT fk.object_id, fk.schema_id, fk.parent_object_id, fc.parent_column_id, t.schema_id AS referenced_schema_id, fk.referenced_object_id, ic.column_id AS referenced_column_id, 1 AS [Level]
        FROM sys.foreign_keys fk
            INNER JOIN sys.tables t ON fk.referenced_object_id = t.object_id
            INNER JOIN sys.foreign_key_columns fc ON fk.object_id = fc.constraint_object_id
            INNER JOIN sys.indexes i ON fk.referenced_object_id = i.object_id AND i.is_primary_key = 1
            INNER JOIN sys.index_columns ic ON i.object_id = ic.object_id AND i.index_id = ic.index_id
        WHERE fk.type = 'F' 
            AND fk.referenced_object_id = OBJECT_ID(N'dbo.Users', N'U')

        UNION ALL

        SELECT fk.object_id, fk.schema_id, fk.parent_object_id, fc.parent_column_id, t.schema_id, fk.referenced_object_id, ic.column_id AS referenced_column_id, cte.[Level] + 1
        FROM sys.foreign_keys fk     
            INNER JOIN sys.tables t ON fk.referenced_object_id = t.object_id
            INNER JOIN sys.foreign_key_columns fc ON fk.object_id = fc.constraint_object_id
            INNER JOIN sys.indexes i ON fk.referenced_object_id = i.object_id AND i.is_primary_key = 1
            INNER JOIN sys.index_columns ic ON i.object_id = ic.object_id AND i.index_id = ic.index_id
            INNER JOIN cte ON fk.referenced_object_id = cte.parent_object_id
        WHERE fk.type = 'F' 
            AND fk.referenced_object_id <> cte.referenced_object_id
    ),
    cteHierarchy AS (
        SELECT DISTINCT
            OBJECT_NAME(cte.object_id) AS ReferringKey,
            SCHEMA_NAME(cte.schema_id) AS ReferringSchema,
            OBJECT_NAME(cte.parent_object_id) as ReferringTable,
            cte.parent_object_id AS reftableid,
            COL_NAME(cte.parent_object_id,cte.parent_column_id) ReferringColumn,
            SCHEMA_NAME(cte.referenced_schema_id) AS ReferencedSchema,
            OBJECT_NAME(cte.referenced_object_id) as ReferencedTable,
            COL_NAME(cte.referenced_object_id,cte.referenced_column_id) ReferencedColumn,
            [Level]
        FROM cte
    )
    SELECT ReferringKey, ReferringSchema, ReferringTable, ReferringColumn, ReferencedSchema, ReferencedTable, ReferencedColumn, [Level]
    FROM cteHierarchy
    ORDER BY [Level], ReferencedSchema, ReferencedTable, ReferringTable;

样品数据如下。 DML对于这个问题不是必需的,但要提供详尽的信息。

CREATE TABLE Users
(
    userid INT NOT NULL,
    CONSTRAINT PK_Users PRIMARY KEY CLUSTERED (userid)
);

CREATE TABLE Client
(
    clientid INT NOT NULL,
    userid INT NOT NULL,
    CONSTRAINT PK_Client PRIMARY KEY CLUSTERED (clientid),
    CONSTRAINT FK_Client_Users FOREIGN KEY (userid) REFERENCES Users (userid)
);

CREATE TABLE Budget
(
    budgetid INT NOT NULL,
    CONSTRAINT PK_Budget PRIMARY KEY CLUSTERED (budgetid)
);

CREATE TABLE BudgetClient
(
    id INT NOT NULL IDENTITY(1,1),
    budgetid INT NOT NULL,
    clientid INT NOT NULL,
    CONSTRAINT PK_BudgetClient PRIMARY KEY CLUSTERED (id),
    CONSTRAINT FK_BudgetClient_Budget FOREIGN KEY (budgetid) REFERENCES Budget (budgetid),
    CONSTRAINT FK_BudgetClient_Client FOREIGN KEY (clientid) REFERENCES Client (clientid)
);

CREATE TABLE Company
(
    companyid INT NOT NULL,
    userid INT NOT NULL,
    CONSTRAINT PK_Company PRIMARY KEY CLUSTERED (companyid),
    CONSTRAINT FK_Company_Users FOREIGN KEY (userid) REFERENCES Users (userid)
);

CREATE TABLE CompanyType
(
    companytypeid INT NOT NULL,
    companyid INT NOT NULL,
    CONSTRAINT PK_CompanyType PRIMARY KEY CLUSTERED (companytypeid),
    CONSTRAINT FK_CompanyType_Company FOREIGN KEY (companyid) REFERENCES Company (companyid)
);

CREATE TABLE CompanyTypeCategory
(
    companytypecategoryid INT NOT NULL,
    companytypeid INT NOT NULL,
    CONSTRAINT PK_CompanyTypeCategory PRIMARY KEY CLUSTERED (companytypecategoryid),
    CONSTRAINT FK_CompanyTypeCategory_CompanyType FOREIGN KEY (companytypeid) REFERENCES CompanyType (companytypeid),
);

INSERT INTO Users (userid)
    VALUES (1),(2),(3),(4),(5),(6),(7),(8),(9),(10);

INSERT INTO Client (clientid,userid)
    VALUES (6,10),(7,3),(8,8),(9,5),(10,6);

INSERT INTO Budget (budgetid)
    VALUES (15),(16),(17),(18),(19),(20),(21),(22),(23),(24),(25);

INSERT INTO BudgetClient (budgetid,clientid)
    VALUES (15,6),(16,7),(17,8),(19,9),(18,10),(21,6),(20,7),(25,8),(23,9),(24,10);

INSERT INTO Company (companyid,userid)
    VALUES (1,7),(5,1),(8,9),(12,2),(15,4);

INSERT INTO CompanyType (companytypeid, companyid)
    VALUES (1,1),(2,5),(3,8),(4,12),(5,15),(6,5),(7,8);

INSERT INTO CompanyTypeCategory (companytypecategoryid,companytypeid)
    VALUES (1,7),(2,1),(3,3),(4,2),(5,1),(6,1),(7,5),(8,4),(9,6),(10,7);

除“类型”(Type)列外,当前返回白/绿线。绿线是多对多表,红线也是我想要返回的表(另一条“一对多”与绿色的“ BudgetClient”表有关)。本质上,递归使用外键来跟踪表关系。因此,这里的顶部/锚表是“用户”,它有两个子表-客户表和公司表,两者都是一对多的关系。反过来,“客户”有一个与“预算”成多对多关系的相关表“预算客户”-BudgetClient将“客户与预算”联系在一起。

要清楚,我希望得到的结果是将返回下图中的所有行。

enter image description here

0 个答案:

没有答案