How to indentify List of table dependencies in a SQL database (SQL 2017) ?

kkran 831 Reputation points
2021-01-12T23:04:56.52+00:00

Hello Everyone - Happy New year!!

I have listed down the list of tables and views using the Information_Schema.Tables.

Select * from Information_Schema.Tables.
where table_catalog = 'databasename'

There are about 200 tables and 30 views.

Now, I want to get the list of dependencies for these tables which got in the above query. I don't want to hard code each table name.

This is what I am looking for :

DatabaseName TableName SchemaNameof table Type (Function, StoredProcedure, View) Name of (Function, StoredProcedure, View)

Thanks

Developer technologies | Transact-SQL
SQL Server | Other
{count} votes

Accepted answer
  1. MelissaMa-MSFT 24,221 Reputation points
    2021-01-13T03:20:36.153+00:00

    Hi @kkran ,

    Happy New Year!

    Please refer below query and check whether it is helpful to you.

    use [databasename]  
    go  
     
    SELECT  DB_NAME() AS DatabaseName,   
    d1.referenced_entity_name TableName,   
    d1.referenced_schema_name SchemaName,   
    case when o1.type ='P' then 'StoredProcedure'   
    	 when o1.type ='TF' then 'Function'   
    	 when o1.type ='V' then  'View' end Type ,   
    OBJECT_NAME(d1.referencing_id) Name  
    FROM sys.sql_expression_dependencies  d1   
    JOIN sys.objects o   
    ON  d1.referenced_id = o.[object_id]  
    JOIN sys.objects p   
    on OBJECT_NAME(d1.referencing_id)=p.name    
    join sys.objects o1   
    on OBJECT_NAME(d1.referencing_id)=o1.name     
    where  o.type_desc ='USER_TABLE'  
    and o1.type in ('P','TF','V')  
    order by DB_NAME() ,referenced_schema_name,referenced_entity_name  
    

    If you would like to get SQL Server Cross database Dependencies, you could also refer this forum.

    Best regards
    Melissa


    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Erland Sommarskog 121.5K Reputation points MVP Volunteer Moderator
    2021-01-12T23:11:48.18+00:00

    It's harder than you think. But I have an article on my web site that discusses the options: http://www.sommarskog.se/sqlutil/SearchCode.html.

    0 comments No comments

  2. Olaf Helper 47,436 Reputation points
    2021-01-13T07:21:24.337+00:00

    See TechNet ScriptCenter List object dependencies (SQL Server 2008) for a T-SQL script.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.