Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 

Get your favorite SSC scripts directly in SSMS with the free SQL Scripts addin. Search for scripts directly from SSMS, and instantly access any saved scripts in your SSC briefcase from the favorites tab.
Download now (direct download link)

Object Search

By Sean Smith,

This stored procedure allows you to search any database (or a combination of databases, including all) for a specific string found within column names, object names, and / or object definitions. As well, there are several optional input parameters which can be passed to the procedure to aid in narrowing down your search.

The quickest way to get started is to run the procedure as follows to get a detailed, overall listing of what it can do:

EXECUTE dbo.usp_Object_Search

     @Search_String = N''
    ,@Database_Name = N''

Which will output:

Search string is NULL or empty.

Usage:

    EXECUTE dbo.usp_Object_Search @Search_String, @Database_Name, @Search_Against, @Object_Type, @Exclude_String, @Hit_Limit, @Creation_Source


Input parameters (pass '?' for extended details):

    @Search_String (Mandatory)   : Search value
    @Database_Name (Mandatory)   : Database(s) to search
    @Search_Against (Optional)   : Search Object Name, Column Name, and / or Object Definition
    @Object_Type (Optional)      : Search Object Type(s)
    @Exclude_String (Optional)   : Exclude results which contain "@Exclude_String" value
    @Hit_Limit (Optional)        : Limit the rows returned
    @Creation_Source (Defaulted) : Search objects created by users, SQL Server, or both


Output (certain columns, indicated by an asterisk, will not be returned in the result set if they are not queried / matched against):

    database_name              : Database in which the matched object was found
    object_type                : Object type
    object_description         : Description of the object
    is_ms_shipped              : Indicates if the object was created by SQL Server
    object_name                : Name of the object in which the match was found
    column_name *              : Name of the column in which the match was found (if 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 (if applicable)
    search_criteria_matched_on : Type of match (Object, Column, Definition)
    row_count *                : Total rows (when a table object)
    total_space *              : Disk space allocated to the table object
    space_used *               : Space used by the table object (of the space allocated)
    space_data *               : Data space used by the table
    space_index *              : Index space used by the table
    space_unused *             : Unused space by the 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

     @Search_String = N'something'
    ,@Database_Name = N'tempdb'
    ,@Search_Against = N'?'

Which will output:

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

Or even:

EXECUTE dbo.usp_Object_Search

     @Search_String = N'something'
    ,@Database_Name = N'tempdb'
    ,@Search_Against = NULL
    ,@Object_Type = N'?'

Which will output:

Valid Object Types (use either a single value, multiple values separated by commas, or NULL for "All":

AF : Aggregate Function
C  : CHECK Constraint
D  : Default Or DEFAULT Constraint
F  : FOREIGN KEY Constraint
FN : Scalar Function
FS : Assembly Scalar-Function
FT : Assembly Table-Valued Function
IF : Inlined Table-Valued Function
IT : Internal Table
L  : Log
P  : Stored Procedure
PC : Assembly Stored Procedure
PG : Plan Guide
PK : PRIMARY KEY Constraint
R  : Rule
RF : Replication Filter Stored Procedure
S  : System Table
SN : Synonym
SO : Sequence
SQ : Service Queue
TA : Assembly DML Trigger
TF : Table-Valued Function
TR : Trigger
TT : Table Type
U  : User-Defined Table
UQ : UNIQUE Constraint
V  : View
X  : Extended Stored Procedure

And even:

EXECUTE dbo.usp_Object_Search

     @Search_String = N'something'
    ,@Database_Name = N'tempdb'
    ,@Search_Against = NULL
    ,@Object_Type = NULL
    ,@Exclude_String = NULL
    ,@Hit_Limit = NULL
    ,@Creation_Source = N'?'

Which will output:

Valid Creation Sources:

B : Both (User and System created objects)
S : System created objects only
U : User created objects only (Default)

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

     @Search_String = N'something'
    ,@Database_Name = N'?'

Which will output (for example):

Valid Database Names (use either a single value, multiple values separated by commas, or '*' for "All"):

master
model
msdb
tempdb

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.

Any friendly feedback is always welcome. Enjoy!

Total article views: 4820 | Views in the last 30 days: 4
 
Related Articles
SCRIPT

Script to Find String in Database object

Script that searches every object in a database for a specific string.

SCRIPT

Simple Object and String Search within Objects

This Simple script will help to do a Server wide search for an object name or usage of a String in O...

FORUM

How to Search Name string for space and convert lastname substring

Search Name string for space and convert lastname substring

SCRIPT

Simplest way to search any string in db objects

Easiest way to search for a string in any object within the database.

SCRIPT

Find a string in database objects

A very simple way to find a string in database objects and see where it is located in object definit...

Tags
columns    
definition    
find    
locate    
object    
object name    
object search    
queries    
search    
 
Contribute