Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
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 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

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

Which will output:

Correct Syntax:

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


Input Parameters (pass '?' as a parameter for extended details):

    @Search_String (Mandatory) : Search string 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


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
    object_type                : Object type
    object_description         : Description of the object type
    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

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

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

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: 3865 | Views in the last 30 days: 82
 
Related Articles
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...

SCRIPT

String Search

Search for a string value within columns of data types CHAR, NCHAR, NTEXT, NVARCHAR, TEXT, VARCHAR, ...

SCRIPT

Search objects in Database

Use this script to search objects in a Database. Also used to check a tables column types and detail...

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

Join the most active online SQL Server Community

SQL knowledge, delivered daily, free:

Email address:  

You make SSC a better place

As a member of SQLServerCentral, you get free access to loads of fresh content: thousands of articles and SQL scripts, a library of free eBooks, a weekly database news roundup, a great Q & A platform… And it’s our huge, buzzing community of SQL Server Professionals that makes it such a success.

Join us!

Steve Jones
Editor, SQLServerCentral.com

Already a member? Jump in:

Email address:   Password:   Remember me: Forgotten your password?
Steve Jones