views:

47

answers:

5

Is there any way given a column, which table it belongs using SQL Query?

A: 

yes, Assuming this is SQL server db, you can check the below query -

select [name], object_name(id) from sys.columns where [name] like '%columnname%'

the object_name(id) will give you the table name for your columnname specified.

Sachin Shanbhag
A: 

Try

SELECT OBJECT_NAME(id) FROM syscolumns WHERE [name] = 'mycolumn'

Noel Abrahams
Use the `sys` catalog views in SQL Server 2005 and newer - `sys.columns` instead of `syscolumns`
marc_s
A: 

Using the syscolumns table (and assuming you just have the column name), you can figure out which tables contain a column with that name. But beyond that you might be stuck.

Paddy
Use the `sys` catalog views in SQL Server 2005 and newer - `sys.columns` instead of `syscolumns`
marc_s
A: 

You can try something like this using Sql Server 2005+

SELECT OBJECT_NAME(c.OBJECT_ID) TableName, c.name ColumnName
FROM sys.columns c
WHERE c.name = '<column name>'
astander
A: 

If you are trying to figure out which table a particular column in a query came from then the best bet is to alias all columns at the time you write queries. I would not accept any code in a code review that doesn't do this because it is a pain to figure out later.

HLGEM