How to determine which objects depend on another object in Sql Server

Introduction

Have you ever been in the situation where you needed to make a modification to an existing object in a SQL Server database but were not sure what impact the change would have on other objects? What objects depend on this object you are about to change? It can be a scary thing to make a change to a well-developed database system.

I was in that situation recently where I needed to add a parameter to an existing function. Well this function was referenced by many stored procedures and the last thing I wanted to do was to examine the SQL for each stored procedure manually. As it turns I don't need to. In SQL Server 2005 there is a system view called sys.syscomments which contains a field called "text." This field holds the code used to create an object in the database. By querying this table we are able to create a list of all objects in the database which depend on the object we're making the change to.

In order to make this query useful we'll need to join on a couple of other tables. What we want to know is the name of the schema as well as the object name. Below is the query I use whenever I’m doing a search.

SELECT

s.[name] AS Schema

,o.[name] AS ‘Object’

,o.[type] AS Type

FROM

sys.schemas s

INNER JOIN

sys.objects o ON s.schema_id = o.schema_id

INNER JOIN

sys.syscomments sc ON o.object_id = sc.id

WHERE

sc.text LIKE %<TextToSearchFor>%

ORDER BY

s.[name]

,o.[name];

With this query I can very quickly generate a list of all the objects that will be impacted and must be updated once the change I need to make is implemented.

Tonatiuh    3/26/2008 7:34 AM

Hey Rob,

 

firt one: I think that this blog is great,well second, i have two questions:

 

1.- This works for a table?, for a sp? in general all?

2.- this query run in a new windows directly in master? i mean i try to run this query exactly (of course i change the statments) and i dont find the sys.schemas table, im doing something wrong?

 

.NET Advisor    3/26/2008 8:01 AM

Hey Tonatiuh,

 

Thank you!

 

To answer your questions, yes, this works for all objects. Are you running this against a sql server 2000 database? If so it would be different objects you need to query. The query above is for 2005.

 

Use this:

 

select

o.[name] as 'Object'

,o.[type] as 'Type'

from

dbo.sysobjects o

inner join

dbo.syscomments sc on o.id = sc.id

where

sc.text like '%<TextToSearchFor>%'

Tonatiuh    3/26/2008 8:16 AM

Hey Rob,

firt one: I think that this blog is great,well second, i have two questions:

1.- This works for a table?, for a sp? in general all?

2.- this query run in a new windows directly in master? i mean i try to run this query exactly (of course i change the statments) and i dont find the sys.schemas table, im doing something wrong?

Thanks

Mario    4/6/2008 11:48 AM

Hi rob, this is mario tona's friend, well i try find a forum here your blog, in this the only place that i find, well....this is my question....

 

what's the difference between blocking and locking in SQL and how can i detected those issues in my server. i undertand more or less the difference but, i dont find the difference in my sql monitor.

 

Thanks Rob.

New Comment

  
  
  
  
Email Print