Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6380061
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T02:13:07+00:00 2026-05-25T02:13:07+00:00

I have a system with multiple databases and client applications. All databases are at

  • 0

I have a system with multiple databases and client applications. All databases are at one SQL Server instance. They have been developed by different people at different time. So if some error occur it is pritty hard to find in which procedure or trigger the data was modified.

Now I use this script, which I found on this site:

SELECT  DISTINCT ISNULL(sd.referenced_schema_name+'.','')+ OBJECT_NAME(sd.referenced_id)TableName,
        OBJECT_NAME(sd.referencing_id)Ref_Object,
        CASE WHEN OBJECTPROPERTYEX(sd.referencing_id,N'IsUserTable')= 1
                     THEN'Table'
        WHEN OBJECTPROPERTYEX(sd.referencing_id,N'IsTableFunction')= 1
                     THEN'Function'
        WHEN OBJECTPROPERTYEX(sd.referencing_id,N'IsTableFunction')= 1
                     THEN'Function'
        WHEN OBJECTPROPERTYEX(sd.referencing_id,N'IsScalarFunction')=1
                     THEN'Function'
        WHEN OBJECTPROPERTYEX(sd.referencing_id,N'IsTrigger')= 1
                     THEN'Trigger'
        WHEN OBJECTPROPERTYEX(sd.referencing_id,N'IsView')= 1
                     THEN'View'
        WHEN OBJECTPROPERTYEX(sd.referencing_id,N'IsUserTable')= 1
                     THEN'Table'
        WHEN OBJECTPROPERTYEX(sd.referencing_id,N'IsProcedure')= 1
                     THEN'Procedure'
        WHEN OBJECTPROPERTYEX(sd.referencing_id,N'IsIndexed')= 1
                     THEN'Index'
        WHEN OBJECTPROPERTYEX(sd.referencing_id,N'IsForeignKey')= 1
                     THEN'ForeignKey'
        WHEN OBJECTPROPERTYEX(sd.referencing_id,N'IsPrimaryKey')= 1
                     THEN'PrimaryKey'
        END AS Ref_Object_Name
FROM    sys.sql_expression_dependencies SD
        INNER JOIN sys.objects obj
                     ON obj.object_id=sd.referenced_id
WHERE   obj.is_ms_shipped= 0
        and referenced_id=object_id('TABLE_NAME') /*Where one can Replace table Name*/
        AND obj.type_desc='USER_TABLE'
        ORDER BY TableName,Ref_Object,Ref_Object_Name

But this script seems to work only for the database to which the table belong.

I want to get for a specified table name (or even better for an object) list of all objects of all databases in which the specified table name met:

Database_Name  SchemaName  ObjectName  ObjectKind  

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T02:13:07+00:00Added an answer on May 25, 2026 at 2:13 am
    DECLARE @table_name SYSNAME = N'%';
    
    
    
    DECLARE @sql NVARCHAR(MAX) = N'';
    
    SELECT @sql += 'SELECT DISTINCT Database_Name = ''' + QUOTENAME(name) + ''',
            COALESCE(sd.referenced_schema_name +''.'', '''')+ o.name AS TableName,
            r.name AS Ref_Object,
            r.type_desc AS Ref_Object_Name
    FROM    ' + QUOTENAME(name) + '.sys.sql_expression_dependencies AS sd
            INNER JOIN ' + QUOTENAME(name) + '.sys.objects AS o
            ON o.object_id = sd.referenced_id
            INNER JOIN ' + QUOTENAME(name) + '.sys.objects AS r
            ON sd.referencing_id = r.object_id
    WHERE   o.is_ms_shipped =  0
            and referenced_id = o.object_id
            AND o.type_desc = ''USER_TABLE''
            AND o.name LIKE ''' + @table_name + '''
    UNION ALL
    '
     FROM sys.databases 
     WHERE database_id BETWEEN 5 AND 32766;
    
    SET @sql = LEFT(@sql, LEN(@sql)-11) 
        + 'ORDER BY Database_Name, TableName,Ref_Object,Ref_Object_Name';
    
    EXEC sp_executesql @sql;
    

    EDIT

    The above will find all the references within each database, but won’t find cross-database references. It took a little playing, and the output isn’t precisely what you wanted, but I think it makes it more self-explanatory:

    DECLARE @table_name SYSNAME = N'%'; -- find all
    
    
    
    CREATE TABLE #d
    (
        db SYSNAME, 
        [object_id] INT, 
        sch SYSNAME,
        obj SYSNAME,
        ref_db NVARCHAR(128),
        ref_sch NVARCHAR(128), 
        ref_obj NVARCHAR(128),
        ref_object_id INT,
        type_desc SYSNAME
    );
    
    DECLARE @sql NVARCHAR(MAX) = N'';
    
    SELECT @sql += 'SELECT ''' + QUOTENAME(name) + ''',
        d.referencing_id,
        QUOTENAME(s.name),
        QUOTENAME(o.name),
        QUOTENAME(d.referenced_database_name),
        QUOTENAME(d.referenced_schema_name),
        QUOTENAME(d.referenced_entity_name),
        d.referenced_id,
        o.type_desc
    FROM ' + QUOTENAME(name) 
        + '.sys.sql_expression_dependencies AS d
    INNER JOIN ' + QUOTENAME(name) 
        + '.sys.objects AS o
        ON d.referencing_id = o.[object_id]
    INNER JOIN ' 
        + QUOTENAME(name) + '.sys.schemas AS s
        ON o.[schema_id] = s.[schema_id]
    WHERE d.referenced_entity_name LIKE ''' + @table_name + '''
    UNION ALL
    '
    FROM sys.databases WHERE database_id BETWEEN 5 AND 32766;
    
    SET @sql = LEFT(@sql, LEN(@sql)-11);
    
    INSERT #d EXEC sp_executesql @sql;
    
    SELECT 
        db+'.'+sch+'.'+obj, 
        ' (' + type_desc + ') references => ',
        COALESCE(ref_db, db)+'.'+ref_sch+'.'+ref_obj
        FROM #d;
    GO
    
    DROP TABLE #d;
    GO
    

    Sample output:

    [db1].[dbo].[foo]  (SQL_STORED_PROCEDURE) references =>   [db2].[dbo].[bar]
    [db1].[dbo].[xyz]  (SQL_STORED_PROCEDURE) references =>   [db1].[dbo].[table_xyz]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a system which contains multiple applications connected together using JMS and Spring
I have a system that takes Samples. I have multiple client threads in the
I have multiple client databases that I need to hit on the fly and
I have a System DSN configured and test ok : Microsoft SQL Server Native
I have been asked to develop a system that collects data from a Sql
To have a general-purpose documentation system that can extract inline documentation of multiple languages,
Is this possible? In an events system an event can have multiple times. (ie,
I have System.Collections.Generic.Dictionary<A, B> dict where A and B are classes, and an instance
I have a system where I query a REST / Atom server for documents.
I am writing tests for our system that does joins across multiple databases. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.