Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find an object in SQL Server (cross-database)

If I've been told a table (or proc) name, but not which connected database the object is located in, is there any simple script to search for it? Maybe search somewhere in the System Databases? (I'm using SQL Server 2005)

like image 689
Margaret Avatar asked Jun 21 '09 23:06

Margaret


People also ask

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.

How do I find dependent objects in SQL Server?

Using SQL Server Management Studio In Object Explorer, expand Databases, expand a database, and then expand Tables. Right-click a table, and then click View Dependencies. In the Object Dependencies<object name> dialog box, select either Objects that depend on <object name>, or Objects on which<object name>depends.

How do I find the object ID in SQL Server?

objects catalog view, obtain the object identification numbers by querying the appropriate catalog view. For example, to return the object identification number of a DDL trigger, use SELECT OBJECT_ID FROM sys. triggers WHERE name = 'DatabaseTriggerLog``' .


2 Answers

There is a schema called INFORMATION_SCHEMA schema which contains a set of views on tables from the SYS schema that you can query to get what you want.

A major upside of the INFORMATION_SCHEMA is that the object names are very query friendly and user readable. The downside of the INFORMATION_SCHEMA is that you have to write one query for each type of object.

The Sys schema may seem a little cryptic initially, but it has all the same information (and more) in a single spot.

You'd start with a table called SysObjects (each database has one) that has the names of all objects and their types.

One could search in a database as follows:

Select [name] as ObjectName, Type as ObjectType From Sys.Objects Where 1=1     and [Name] like '%YourObjectName%' 

Now, if you wanted to restrict this to only search for tables and stored procs, you would do

Select [name] as ObjectName, Type as ObjectType From Sys.Objects Where 1=1     and [Name] like '%YourObjectName%'     and Type in ('U', 'P') 

If you look up object types, you will find a whole list for views, triggers, etc.

Now, if you want to search for this in each database, you will have to iterate through the databases. You can do one of the following:

If you want to search through each database without any clauses, then use the sp_MSforeachdb as shown in an answer here.

If you only want to search specific databases, use the "USE DBName" and then search command.

You will benefit greatly from having it parameterized in that case. Note that the name of the database you are searching in will have to be replaced in each query (DatabaseOne, DatabaseTwo...). Check this out:

Declare @ObjectName VarChar (100)  Set @ObjectName = '%Customer%'  Select 'DatabaseOne' as DatabaseName, [name] as ObjectName, Type as ObjectType From DatabaseOne.Sys.Objects Where 1=1     and [Name] like @ObjectName     and Type in ('U', 'P')  UNION ALL  Select 'DatabaseTwo' as DatabaseName, [name] as ObjectName, Type as ObjectType From DatabaseTwo.Sys.Objects Where 1=1     and [Name] like @ObjectName     and Type in ('U', 'P')  UNION ALL  Select 'DatabaseThree' as DatabaseName, [name] as ObjectName, Type as ObjectType From DatabaseThree.Sys.Objects Where 1=1     and [Name] like @ObjectName     and Type in ('U', 'P') 
like image 102
Raj More Avatar answered Oct 22 '22 13:10

Raj More


sp_MSforeachdb 'select db_name(), * From ?..sysobjects where xtype in (''U'', ''P'') And name = ''ObjectName''' 

Instead of 'ObjectName' insert object you are looking for. First column will display name of database where object is located at.

like image 35
Andrija Avatar answered Oct 22 '22 13:10

Andrija