views:

1006

answers:

3

Hi,

Please can someone show me how I would return the column names of a table using SQL server 2008?

i.e. a table contains the columns id, name, address, country and I want to return these as data?

Thank you

A: 

Something like this?

sp_columns @table_name=your table name
Paul Lefebvre
+5  A: 

Not sure if there is an easier way in 2008 version.

select column_name,* from information_schema.columns
 where table_name = 'YourTableName'
Gulzar
A: 

One method is to query syscolumns:

select
   syscolumns.name as [Column],
   syscolumns.xusertype as [Type],
   sysobjects.xtype as [Objtype]
from 
   sysobjects, syscolumns 
where sysobjects.id = syscolumns.id
and   sysobjects.xtype = 'u'
and   sysobjects.name = 'MyTableName'
order by syscolumns.name
splattne