Thursday, November 11, 2010

How do I find an object/objects which are referencing a particular table

Recently I have been assigned a task to find out all the tables, stored procs, views where are making reference to a particular table.


Query to find out all the tables which are referencing a particular table -

DECLARE @objectname varchar(256)
SET @objectname = 'TB_CON_LogicalOperator'

SELECT OBJECT_NAME(referenced_id)
FROM sys.sql_expression_dependencies
WHERE referencing_id = OBJECT_ID(@objectname)


Query to find out stored procs/views which are referencing a particular table -

DECLARE @objectname varchar(256)
SET @objectname = 'TB_CON_LogicalOperator'

SELECT OBJECT_NAME(referencing_id)
FROM sys.sql_expression_dependencies
WHERE referenced_id = OBJECT_ID(@objectname)


Query to find out views which are referencing a particular table -

DECLARE @objectname varchar(256)
SET @objectname = 'TB_CON_LogicalOperator'

SELECT name
FROM sys.views
WHERE OBJECT_DEFINITION(OBJECT_ID) LIKE '%'+@objectname+'%'

No comments: