Determine table referenced in a view in SQL Server

后端 未结 4 680
终归单人心
终归单人心 2020-12-29 14:08

How can i get to know the tables used in a view in SQL Server? Is there a script or a tool that could let me know the tables used in a view and can also list down the fields

4条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-29 14:18

    This information is available from the INFORMATION_SCHEMA

    SELECT * 
        FROM    INFORMATION_SCHEMA.VIEW_COLUMN_USAGE AS cu
        JOIN    INFORMATION_SCHEMA.COLUMNS AS c
        ON      c.TABLE_SCHEMA  = cu.TABLE_SCHEMA
        AND     c.TABLE_CATALOG = cu.TABLE_CATALOG
        AND     c.TABLE_NAME    = cu.TABLE_NAME
        AND     c.COLUMN_NAME   = cu.COLUMN_NAME
        WHERE   cu.VIEW_NAME    = 'viewtablename';
    

提交回复
热议问题