views:

58

answers:

5

I'm trying to find all references to an object in an SQL Server database.

How can I quickly search? SQL Server Management Studio does not seem to do it. I use http://www.red-gate.com/products/SQL_Search/ but I'd like to find the "official" Microsoft solution to this. Is it in another product?

For example, when I do a mass search in visual studio, I would like to be able to also find something in all stored procedures.

Or maybe I'm not coding this the right way?

Carl

A: 

I'm not sure of 'official microsoft' way but I've used SqlDigger in the past. It's not bad.

If you want to do it in VS, then you will need the text of all your procs included in your project.

BioBuckyBall
A: 

I use this query to look for all tables (or text) in the stored procedures:

SELECT DISTINCT o.name, o.xtype
FROM syscomments c
INNER JOIN sysobjects o ON c.id=o.id
WHERE c.TEXT LIKE '%tablename%'
Martin
See my answer for why you should be checking `SYS.SQL_MODULES` instead.
OMG Ponies
+4  A: 

Use:

select object_name(m.object_id), m.*
  from sys.sql_modules m
 where m.definition like N'%name_of_object%'

...because SYSCOMMENTS and INFORMATION_SCHEMA.routines have nvarchar(4000) columns. So if "name_of_object" is used at position 3998, it won't be found. SYSCOMMENTS does have multiple lines, but INFORMATION_SCHEMA.routines truncates.

OMG Ponies
Thanks ... though I wish you would have told me this a couple of weeks ago!!!
Martin
@MArtin the question was only asked 20 hours ago...
George Stocker
A: 

There is a way to do this in SQL 2000 with the syscomments table and still get accurate results, as long as the string searched for is not longer than about 2000 characters. If anyone wants to know this, please post a comment and I'll dig up the code for you.

Emtucifor
A: 

SQL Server Management Studio has a View Dependencies feature when you right click on an object in the Object Explorer. Is this what you're looking for?

David Atkinson