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
2010-10-13 10:47:31
A:
Try
SELECT OBJECT_NAME(id)
FROM syscolumns
WHERE [name] = 'mycolumn'
Noel Abrahams
2010-10-13 10:48:46
Use the `sys` catalog views in SQL Server 2005 and newer - `sys.columns` instead of `syscolumns`
marc_s
2010-10-13 10:54:04
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
2010-10-13 10:50:42
Use the `sys` catalog views in SQL Server 2005 and newer - `sys.columns` instead of `syscolumns`
marc_s
2010-10-13 10:54:32
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
2010-10-13 10:50:53
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
2010-10-13 15:40:10