2017-10-05 50 views
2

我想查看所有視圖的所有依賴關係,並且還有一個路徑的錨點字段。視圖依賴關係和譜系的遞歸查詢

我已經到了這裏,通過刮取視圖定義來創建TEST_RECURSIVE_SEARCH。

CREATE MULTISET VOLATILE TABLE TEST_RECURSIVE_SEARCH 

    (Databasename VARCHAR(100), 

    EntityName VARCHAR(100), 

    RLTD_Databasename VARCHAR(100), 

    RLTD_EntityName VARCHAR(100), 

    RLTD_REASON VARCHAR(100) 

) 

PRIMARY INDEX (databasename, entityName) 

ON COMMIT PRESERVE ROWS; 

INSERT INTO TEST_RECURSIVE_SEARCH VALUES ('DB1','TopLevelEntity','DB1','SecondLevelEntity','READS FROM'); 

INSERT INTO TEST_RECURSIVE_SEARCH VALUES ('DB1','SecondLevelEntity','DB1','ThirdLevelEntity','READS FROM'); 

INSERT INTO TEST_RECURSIVE_SEARCH VALUES ('DB1','ThirdLevelEntity','DB1','FourthLevelEntity','READS FROM'); 

INSERT INTO TEST_RECURSIVE_SEARCH VALUES ('DB1','FourthLevelEntity','DB1','FifthLevelEntity','READS FROM'); 



WITH RECURSIVE REC_SUR 

    (DatabaseName, 

    EntityName, 

    NestDependentDB, 

    NestedDependentEntity 

) AS 

    (SELECT TRS1.Databasename, 

      TRS1.EntityName, 

      TRS2.databasename, 

      TRS2.EntityName 

     FROM TEST_RECURSIVE_SEARCH AS TRS1 

    INNER 

     JOIN TEST_RECURSIVE_SEARCH AS TRS2 

     ON TRS1.Rltd_databasename = TRS2.Databasename 

     AND TRS1.Rltd_entityName = TRS2.entityName 

    UNION 

     ALL 

    SELECT REC_SUR.Databasename, 

      REC_SUR.EntityName, 

      TRSN.rltd_databasename, 

      TRSN.Rltd_EntityName 

     FROM REC_SUR 

    INNER 

     JOIN TEST_RECURSIVE_SEARCH AS TRSN 

     ON REC_SUR.NestDependentDB = TRSN.Databasename 

     AND REC_SUR.NestedDependentEntity = TRSN.entityName 

) 

SELECT * 

    FROM REC_SUR 

-- Pick up the last level which won't have a relationship 

UNION 

    ALL 

SELECT TRS1.databasename, 

     TRS1.entityName, 

     TRS1.rltd_databasename, 

     TRS1.rltd_entityName 

    FROM TEST_RECURSIVE_SEARCH AS TRS1 

    LEFT 

    JOIN TEST_RECURSIVE_SEARCH AS TRS2 

    ON TRS1.Rltd_databasename = TRS2.Databasename 

    AND TRS1.Rltd_entityName = TRS2.entityName 

    WHERE TRS2.databasename IS NULL); 

這給了我所有依賴視圖的實體,但沒有上下文或方式來回溯路徑。

我試圖讓這個作爲輸出:

DatabaseName EntityName  NestDependentDB NestedDependentEntity DependentThroughEntity 

DB1  FourthLevelEntity  DB1  FifthLevelEntity FourthLevelEntity   

DB1  SecondLevelEntity  DB1  ThirdLevelEntity SecondLevelEntity  

DB1  SecondLevelEntity  DB1  FourthLevelEntity ThirdLevelEntity  

DB1  ThirdLevelEntity  DB1  FourthLevelEntity ThirdLevelEntity  

DB1  ThirdLevelEntity  DB1  FifthLevelEntity FourthLevelEntity  

DB1  SecondLevelEntity  DB1  FifthLevelEntity FourthLevelEntity  

DB1  TopLevelEntity   DB1  FifthLevelEntity FourthLevelEntity  

DB1  TopLevelEntity   DB1  FourthLevelEntity ThirdLevelEntity  

DB1  TopLevelEntity   DB1  SecondLevelEntity TopLevelEntity   

DB1  TopLevelEntity   DB1  ThirdLevelEntity SecondLevelEntity  

另外,如果您對如何消除UNION任何想法拿起底層記錄我會很感激這一點。

回答

1

這應該完成與DBC層次結構類似的東西:

WITH RECURSIVE cte (DatabaseName, Path, Parent, Level) AS 
(
SELECT TRIM(d1.DatabaseName) 
     , d1.DatabaseName (VARCHAR(625)) 
     , TRIM(d1.DatabaseName) 
     , 0 (BYTEINT) 
    FROM DBC.DatabasesV d1 
    WHERE DatabaseName = DBC 

UNION ALL 

SELECT TRIM(d2.DatabaseName) 
     , cte.Path || '.' || TRIM(d2.DatabaseName) 
     , cte.Path 
     , cte.Level + 1 
    FROM DBC.DatabasesV d2 
     , cte 
    WHERE d.OwnerName = cte.DatabaseName 
    AND d.DatabaseName <> d.OwnerName 
    AND cte.Level < 20 -- This is a failsafe for the recursion 
) 
SELECT Level 
    , SUBSTRING(CAST('' AS CHAR(60) FROM 1 FOR Level * 2) || DatabaseName AS Hierarchy 
    , Path 
    , Parent 
    FROM cte 
ORDER BY Path; 
+1

尼斯,這實際上是比什麼,我要求,因爲它有完整的路徑更好。 –