Stored procedure to compensate for Microsoft's lack of an Object Search Utility; previously found in SQL Server 2000. Woks on SQL Server 2005 and 2008.
In short, you can search any database (or a combination of databases, including all) for a specific string found in column names, object names, and /or object definitions. As well, there are several optional parameters which can be passed to the procedure to aid in narrowing down your search.
The easiest thing to start with is to run the procedure as follows to get a detailed, overall listing of what it can do:
EXECUTE [dbo].[usp_Object_Search] '',''
Which will give you:
Msg 50000, Level 16, State 1, Procedure usp_Object_Search, Line 203
ERROR: Search string is NULL or empty.
Correct Syntax:
dbo.usp_object_search @v_Search_String, @v_Database_Name, @v_Search_Against, @v_XType, @v_Exclude_String, @v_Hit_Limit
Input Parameters: (pass '?' as a parameter for extended details; optional parameters accept NULLs):
@v_Search_String (Mandatory) : Search string value
@v_Database_Name (Mandatory) : Database(s) to search
@v_Search_Against (Optional) : Search Object Name, Column Name, and / or Object Definition
@v_XType (Optional) : Search Object Type(s)
@v_Exclude_String (Optional) : Exclude results which contain @v_Exclude_String value
@v_Hit_Limit (Optional) : Limit the rows returned
Output (certain columns, indicated by an asterisk, will not be returned in the result set if they are not queried / matched against):
database_name : Name of the database in which the matched object was found
xtype : XType code of the object
object_type : XType description of the object
object_name : Name of the object in which the match was found
column_name * : Name of the column in which the match was found (when applicable)
data_type * : Data type of the "column_name" field
data_length * : Data length of the "data_type" field
definition * : Definition details in which the match was found (when applicable)
search_criteria_matched_on : Indicates type of match (Object, Column, Definition, etc.)
row_count * : Total records in a table object
total_space * : Total disk space allocated to a table object
space_used * : Total space used by a table object (of the space allocated)
space_data * : Total space used by a table object attributed to data
space_index * : Total space used by a table object attributed to indexes
space_unused * : Total space unused by a table object (of the space allocated)
Additionally, you can get specific details on parameter options by passing a question mark:
EXECUTE [dbo].[usp_Object_Search] 'MySearchString','MyDatabase','?'
Which will give you (for example):
Msg 50000, Level 16, State 1, Procedure usp_Object_Search, Line 325
ERROR: '?' is not a valid search type.
Valid Search Types (use single characters, combinations such as 'CD', 'NC', etc., or NULL for "ALL"):
C : Search Column Names
D : Search Object Definitions
N : Search Object Names
You can also get a quick listing of all the available databases you can query by intentionally passing a non-existent database name as a parameter:
EXECUTE [dbo].[usp_Object_Search] 'MySeachString','?'
Which will give you (for example):
Msg 50000, Level 16, State 1, Procedure usp_Object_Search, Line 300
ERROR: Database '?' not found on server.
Valid Database Names (use either a single value, multiple values separated by commas, or '*' for "All" (NOT recommended):
MyDatabase1
MyDatabase2
MyDatabase3
MyDatabase4
MyDatabase5
MyDatabase6
MyDatabase7
I've found the procedure to be very handy when making changes to a production database / server and I want to see which Objects might be affected before going live.