19

How do I list all the tables in a database that contain a column with a given name?

Tim B
  • 40,716
  • 16
  • 83
  • 128
heena
  • 191
  • 1
  • 1
  • 3
  • 1
    Can you be more specific? Do you want a list of columns in a table, or are you looking for all tables that contain a column, or something else? – Jonathan B Jan 03 '11 at 14:56

4 Answers4

24

Try this:

select distinct object_name(id)
from syscolumns
where name = 'my_column_name'
order by object_name(id)

or this way:

select distinct so.name
from syscolumns sc
  , sysobjects so
where sc.id = so.id
and   sc.name = 'my_column_name'
order by so.name

is that what you've been looking for?

B0rG
  • 1,215
  • 12
  • 13
4

You can also use sp_columns stored procedure.

sp_columns @column_name = '%column_name%'

More info here: SyBooks Online (Getting Help On Database Objects)

Miguel
  • 1,575
  • 1
  • 27
  • 31
3

The below query can be used to get column_name with table_name as well.

This will help the user identify the table to which the column_name belongs.

select distinct tbl_col.name as Field_name,tbl_object.name as Table_name
from syscolumns tbl_col
  , sysobjects tbl_object
where tbl_col.id = tbl_object.id
order by tbl_object.name
SWalters
  • 3,615
  • 5
  • 30
  • 37
Amol
  • 31
  • 2
0

Try this:

sp_columns @column_name = '%column_name%'
Raunak Jhawar
  • 1,541
  • 1
  • 12
  • 21