SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 

Find for SQL Objects, Entities or Text

By Sunil Chandurkar, 2008/07/03

Total article views: 1547 | Views in the last 30 days: 24

This Stopred Procedure will help you to find database objects such as tables, stored procedures, views or function by its name. You can find for table(s) having a specific column.

While working on project we often do impact analysis. Most of the changes in application directly affect the database. Hence we need to figure out the tables having a particular column and the stored procedures using the column in the quries written inside them. I used to do the same task in my project work.

To minimize the time effort involved in such tasks I though to have a basic tool which will perform a quick search for me and return list of almost all objects (tables, stored procedures, views....) mathcing the specified condition.

So I created a stored procedure which will do the job for me.

Lets have a look at the code one by one.

Stored Procedure Name: uspFindObjects


Parameters of Stored Procedure.


1. @ObjectType

Type of object to search. You search for a table or stored procedure by its name. Following @ObjectType can be passed to stored procedure.

- U = Table
- P = Stored Procedure
- V = View
- FN = Function
- * = Find All (Tables, SP, Views, Functions)


2. @SearchText

Text to search


3. @SearchType

Type of data to search

- C = Search for a column

- T = Search for a text


4. @OrderBy
This specifies the field by which order the result should be returned. This parameter is optinal. By defult result is return in order of object name (@OrderBy='N'). Anything other by 'N' is Object type.

Lets have an example to understand it better.

DataBase: MyTestDatabase

We have a table Client_Master with following structure.
Table Name : Client_Master

Column Name Data Type
Client_ID NUMERIC (10)
Client_Name VARCHAR (100)

Then lets have a stored procedure spClientList as

CREATE STORED PROCEDURE spClientList AS

( Select Client_ID,Client_Name FROM Client_Master)

Now we can perform search as displayed below:

1. Seach for the table named Client_Master in list of tables

EXEC uspFindObjects 'U','Client_Master','N'

2. Seach for the SP named spClientList in entire database

EXEC uspFindObjects '*','spClientList','N'

3. Seach for the column Client_ID in tables

EXEC uspFindObjects 'U','Client_ID','C'

4. Seach for the column Client_ID in stored procedures

EXEC uspFindObjects 'P','Client_ID','T'

5. Seach for the column Client_ID in all SP/Views/Functions in entire database

EXEC uspFindObjects '*','Client_ID','T'

This way you can search for the specific objects in a database.

I have provided the comments in stored procedure code to understand it better. Hence I will not explain rest fo the code here again.

Hope the code helps you and provides better understanding of how to look for DB objects in system tables.

By Sunil Chandurkar, 2008/07/03

Total article views: 1547 | Views in the last 30 days: 24
Your response
 
 
Related Articles
SCRIPT

Search All Stored Procedures in a Database

Search for a text in all the stored Procedures in a Database

SCRIPT

Search for a String in all Stored Procedures and beyond?

Search for String Occurence with this script in your stored procedures.

FORUM

Identifying if a user/client changes a stored procedure

I want to be able to identify easily if a client has changed a stored procedure.

FORUM

stored procedure---finding all tables that contain a specific column

stored procedure---finding all tables that contain a specific column

FORUM

VSS For Stored Procedures

Saving versions of Stored Procedures

Tags
t-sql    
 
Contribute

Free registration required...

To read the rest of this article, and access thousands of other articles, we ask you to register on the site and subscribe to our newsletters.

Login (existing users)

Login

Email:   Password:   Remember me: Forgotten your password?

Register (new users)

Register

Email:   Password:
Confirm:

Subscribing to our newsletters gets you:

  • ALL of our content (thousands of articles, scripts, and forum postings)
  • A daily newsletter (example)
  • A weekly news round up (example)
  • The opportunity to ask and answer questions in our forums
  • A daily Question of the Day to test and help you increase your knowledge of SQL Server.

Steve Jones
Editor, SQLServerCentral.com