简体   繁体   中英

SQL Server 2008 R2 DMV - sys.dm_sql_referencing_entities - query usage

I am trying to get all dependencies for a list of tables using the sys.dm_sql_referencing_entities DMV.

This query gives me the list of all tables:

SELECT TableName from FinalTableList;  

This query gives the dependencies for TableA:

SELECT  referencing_entity_name
FROM sys.dm_sql_referencing_entities ('dbo.TableA', 'OBJECT') ;  

I would like to combine the above 2 in a query or procedure so that I can get all dependencies in the following format:

TableA  Dependency1
        Dependency2
        Dependency3
TableB  Dependency1
        Dependency2
...................

How should the query be framed?

This is a great example of where you want to use cross apply . Here is an example:

SELECT  tablename, referencing_entity_name
FROM (select 'information_schema.columns' as tablename, 'object' as type) t cross apply
     sys.dm_sql_referencing_entities(t.tablename, t.type)

In the first subquery (which is called t ), you can just list all the pairs of arguments. Then, cross apply will run the function on each of them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM