Wednesday, 16 May 2012

T-SQL script for finding object dependencies

This morning I wanted to find out where a specific stored procedure was being used.  I wasn't sure how to do this using T-SQL so did a bit of googling and found this article.  I have made a slight enhancement to the scripts shown in the article. 

DECLARE @ObjectName VARCHAR(500)
SET @ObjectName = 'dbo.sp_test_Proc'

-- shows objects that the named object depends on
SELECT DISTINCT OBJECT_NAME(DEPID) DEPENDENT_ON_OBJECT, OBJECT_NAME (ID) OBJECTNAME
FROM SYS.SYSDEPENDS
WHERE ID = OBJECT_ID(@ObjectName)

-- shows objects that use the named object
SELECT DISTINCT  OBJECT_NAME(DEPID) OBJECT_NAME, OBJECT_NAME (ID) USED_IN_OBJECT
FROM SYS.SYSDEPENDS
WHERE DEPID = OBJECT_ID(@ObjectName)

I am sure I will get a lot of milage out of this in the future.

No comments:

Post a Comment