Solved

Search T-SQL (views, stored procedures, triggers) for use of a column name.

Posted on 2011-02-15
5
604 Views
Last Modified: 2012-05-11
I'm sure this problem could be solved by documenting things better but...

We've had problems where we changed what a user-defined field was used for or the range of allowed values for a field and we forgot about someplace we were using that field (view, SP, etc.) that would break due to the change.

I'm looking for a way to search through all views, stored procedures, triggers, functions, etc in a given table for use of a particular field.  Searching the field name would suffice, but searching for a field from a particular table would be better.

Thanks.
0
Comment
Question by:ChrisEGP
5 Comments
 
LVL 51

Assisted Solution

by:Huseyin KAHRAMAN
Huseyin KAHRAMAN earned 150 total points
ID: 34899669
try this

select * from sys.all_sql_modules m, sys.all_objects o
where m.object_id=o.object_id
and m.definition like '%Questions%'
0
 
LVL 50

Expert Comment

by:Lowfatspread
ID: 34899677
this information is not available to you from sql server, as you acknowledge its basically down to you to document
your applications/data structures...


you can write search sql to look into your procedure/trigger/function logic  but nothing will assist you in parsing the sql
statements tat you find within so you will not be able to consistently determine where columns or even tables
are referenced... (Tables etc could be aliased in so many ways with many layers present...)


there are

Information_schema.columns /Tables/routines  tables which you can search...
0
 
LVL 51

Assisted Solution

by:Huseyin KAHRAMAN
Huseyin KAHRAMAN earned 150 total points
ID: 34899693
something like this:

declare @myTable varchar(100) = 'myTable';
declare @myColumn varchar(100) = 'myColumn';

select * from sys.all_sql_modules m, sys.all_objects o
where m.object_id=o.object_id
and m.definition like '%'+@myTable+'%'
and m.definition like '%'+@myColumn+'%'
0
 
LVL 70

Accepted Solution

by:
Éric Moreau earned 100 total points
ID: 34900312
0
 

Author Closing Comment

by:ChrisEGP
ID: 34934618
@HainKurt gets points for being the simplest way to get the results.
@emoreau has the best answer because the results from RedGate's SQL Search are much easier to pick through.  It looks like I'll be using this goodie a lot.

Thanks all!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question