Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find all references to an object in an SQL Server database

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

like image 256
Malartre Avatar asked Sep 09 '10 23:09

Malartre


People also ask

How can I see all references in SQL Server?

To view the objects on which a table depends. In Object Explorer, expand Databases, expand a database, and then expand Tables. Right-click a table, and then click View Dependencies.

How do I find database objects in SQL Server?

Search object using SSMS object explorer detailsNavigate to View-> Object Explorer Details in SSMS. You can use a keyboard shortcut F7 to open it. It opens the following screen and shows the various folders – Databases, Security, Server objects, Replication, PolyBase, Always on High Availability.


1 Answers

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.

like image 60
OMG Ponies Avatar answered Sep 20 '22 06:09

OMG Ponies