Sql 视图依赖项和沿袭的递归查询

Sql 视图依赖项和沿袭的递归查询,sql,teradata,Sql,Teradata,我想查看所有视图的所有依赖项,并为路径创建一个锚字段 我已经做到了这一点,这里的TEST\u RECURSIVE\u搜索是通过删除视图定义创建的 CREATE MULTISET VOLATILE TABLE TEST_RECURSIVE_SEARCH ( Databasename VARCHAR(100), EntityName VARCHAR(100), RLTD_Databasename VARCHAR(100), RLTD_EntityName VARC

我想查看所有视图的所有依赖项,并为路径创建一个锚字段

我已经做到了这一点,这里的TEST\u RECURSIVE\u搜索是通过删除视图定义创建的

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     

此外,如果您对如何消除该联合以获取底层记录有任何想法,我也会很感激。

这应该可以实现与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;

很好,这实际上比我要求的要好,因为它有完整的路径。-我不熟悉Teradata,这可能是一个蹩脚的问题,但上面将给出从多个DBS创建的视图的依赖关系视图不存储在数据字典中。我能提供的最好的建议是做一个100万米的精选节目。。。从db.view`