Solved

Query to find a specific field ina a single databse

Posted on 2006-07-05
9
386 Views
Last Modified: 2008-01-09
I am in need of a solution to find a specific field, and how many times it is used, referenced throughout a databse

There are 2064 tables in databse A - I want to find out which tables have the 'company' field in this database.  Is there a query that can be written to find all of the tables that use 'company' as a field in databse A.  I do not care what the data is at this time, just want to know the location of the field without having to query each table.

Thank you in advance for your assistance.

Greg

0
Comment
Question by:Okie13
9 Comments
 
LVL 12

Accepted Solution

by:
Einstine98 earned 225 total points
ID: 17046692
use sysobjects to find the tables
sysdepends to find dependencies
syscolumns to locate a specific column

example

SELET O.Name
FROM sysobjects AS O
JOIN syscolumns AS C
on C.id = O.ID
WHERE C.Name = 'company'
0
 
LVL 42

Assisted Solution

by:EugeneZ
EugeneZ earned 200 total points
ID: 17047574
try:
INFORMATION_SCHEMA.COLUMNs view: http://msdn2.microsoft.com/en-us/library/ms188348.aspx

exec master..sp_msforeachdb 'use ? select * from INFORMATION_SCHEMA.COLUMNs where column_name=''company'''
++++++++++++++++++++++++=
or\and
undocumented solution:

exec master.dbo.sp_MSobjsearch @searchkey = N'company', @objecttype = 1024, @dbname = [*], @casesensitive = 0, @hitlimit = 1000, @status = 1, @extpropname = NULL, @extpropvalue = NULL
0
 
LVL 42

Assisted Solution

by:EugeneZ
EugeneZ earned 200 total points
ID: 17047581
also there is GUI solution:
open Query Analyzer and press F4 (find object)...
0
 
LVL 42

Expert Comment

by:EugeneZ
ID: 17047585
BTW:
the solution for all databases:
exec master..sp_msforeachdb 'use ? select * from INFORMATION_SCHEMA.COLUMNs where column_name=''company'''

you need just:
use yourDBname
select * from INFORMATION_SCHEMA.COLUMNs where column_name='company'
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 75

Assisted Solution

by:Aneesh Retnakaran
Aneesh Retnakaran earned 75 total points
ID: 17047706
SELECT DISTINCT TABLE_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME = 'Company'
0
 
LVL 42

Expert Comment

by:EugeneZ
ID: 17049446
aneeshattingal:
 why do you need "DISTINCT"? How many COLUMN_NAME = 'Company' can be per table?
It is just and One. No need in 'SELECT DISTINCT TABLE_NAME ...'
                                                   --------^----------
0
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 17049577
You are right EugeneZ...
"DISTICT" is not really needed in that.. Also I didn't See your previous post ... .
0
 
LVL 34

Expert Comment

by:James0628
ID: 17049671
You can find a stored procedure named sp_FindColumn at the following URL.  It will let you search for one or more columns by name or partial name, and some other options.

 http://searchsqlserver.techtarget.com/tip/1,289483,sid87_gci1109458,00.html

 James
0
 

Author Comment

by:Okie13
ID: 17050707
Thanks to all of you - each your answers worked perfectly and really helped me out - I gave the most to Einstine98 for being first ... EugeneZ:  Thanks for the GUI tip - I liked that ome since I am in it so often.

As I said all of the answers worked and I really do appreciate it!!

Greg
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
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…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

744 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now