Solved

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

Posted on 2011-02-15
5
591 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:HainKurt
HainKurt 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:HainKurt
HainKurt 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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
why sql server only update some statistics in the database ? 3 22
Sql Server group by 10 29
sql server tables from access 18 22
Merge two rows in SQL 4 14
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how the fundamental information of how to create a table.

803 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