Site Search:
Sign in | Join | Help
4Penny.net

SQL Server (T-SQL)

Comments and notes on SQL Server 2000, 2005, and T-SQL

Find tables that contain a certain field

It is occasionally necessary to find all the occurrences of a field name in a SQL Server database. This script will seacrh the database and return all the tables that contain a certain field name.

select sysobjects.name
from syscolumns
left join sysobjects on sysobjects.id = syscolumns.id
where syscolumns.name like 'myFieldName'
order by 1

Comments

 

Gultig said:

Your statement will also return any views that contain that column.

August 16, 2007 5:35 PM [Delete]
 

Bruno said:

Excellent query, help me a lot!!

November 2, 2007 8:06 AM [Delete]
 

Justin said:

Thanks for the query. Made finding my join columns very easy :)

November 6, 2007 4:04 PM [Delete]
 

nanu said:

Nice solution, thanks a lot

December 27, 2007 3:14 PM [Delete]
 

Johann de Swardt said:

Thanks a lot, does exactly what I need.

February 21, 2008 6:31 AM [Delete]
 

Duane said:

Perfect.

Major thanks

March 4, 2008 1:47 PM [Delete]
 

sendow said:

Another option that I use (because I find it much easier to remember) is:

SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE COLUMN_NAME LIKE '%CUSTNMBR%'

It is also alot simpler to write out than the typical sysobjects query.

You can separately query Views and Tables, but I find that I use Columns most often. (Check out BOL for more options)

I believe there are a few limitations of this approach vs. sysobjects (like trying to find triggers, etc.), but I find it simpler and quicker for 99% of the blind searches that I have to do.

March 4, 2008 6:10 PM [Delete]
 

Jo said:

Excellent  Query!!!!!!

May 7, 2008 7:23 AM [Delete]
 

Ki said:

nice tips ... how about if i wanted to find all table names where COLUMN_NAME LIKE '%CUSTNMBR%'

but also exclude another column.

So 2 clauses.

May 22, 2008 11:16 AM [Delete]
 

whalon said:

Hi All,

Could any of the previous queries be modified to return the table and column name that contain a specific value.  So basically, I know the value I am looking for (varchar), but I do not know the table or column. Also, the value may be in more than one table/column.  

Thanks for the help.  

October 1, 2008 10:12 PM [Delete]
 

ashaank@yahoo.com said:

also it would hav been good to use (nolock) with databases that are running live. Your query will look like this:

select sysobjects.name

from syscolumns (nolock)

left join sysobjects (nolock) on sysobjects.id = syscolumns.id

where syscolumns.name like 'myFieldName'

order by 1

October 20, 2008 7:35 PM [Delete]

Leave a Comment

(required)  
(optional)
(required)  
Add

About Steve Gray

Steve is a seasoned (translate: old) developer in VB and ASP.NET. He spends most of his time in Dynamics GP, writing custom mods for consulting firms. Crystal reports, eConnect, VS Tools for Dynamics... anything that comes along.