Solved

Finding the tables that contain fieldX

Posted on 2000-05-04
5
348 Views
Last Modified: 2008-02-20
I can check syscolumns to find how many tables contain a particular field in a database, but is there a way to pinpoint the tables those fields are in?
0
Comment
Question by:hemphandle
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 2776762
select o.name from sysobjects o join syscolumns c on o.id = c.id where c.Name = "id"

This returns the list of tables in the current database where column name is "id".
Is this what you are looking for?
0
 
LVL 5

Expert Comment

by:dtomyn
ID: 2777108
Perhaps better yet (if angelIII and I am following your question correctly) is to use INFORMATION_SCHEMA.COLUMNS
i.e.
SELECT TABLE_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME = "id"
0
 

Accepted Solution

by:
USP earned 100 total points
ID: 2778219
You can also write the stored procedure which can accept the column name as parameter & generate the list of all the table like...

*--------------*
Create Procedure Sp_FindColumn
@MyCol varchar(255)
As
Select o.name As ResourceTable
From sysobjects o
Join syscolumns c
On o.id = c.id
Where c.Name = @mycol
*----------------*
0
 
LVL 5

Expert Comment

by:dtomyn
ID: 2778273
Gee thanks USP, your "Answer" is SO much better than mine and ESPECIALLY angelIII's!

Thanks again for adding your extreme insight on this one.  You really deserve those points!
0
 
LVL 4

Expert Comment

by:Gustavo Perez Buenrostro
ID: 2789206
hemphandle,
Check this one (Better and faster):

create proc spFindCol
@snColName sysname=null
as
begin
  set nocount on
  select object_name(id)
    from syscolumns
   where name=@snColName
end
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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Viewers will learn how the fundamental information of how to create a table.
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.

737 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