In SQL Server, you can select COLUMN_NAME from INFORMATION_SCHEMA. COLUMNS .
Not sure if there is an easier way in 2008 version.
USE [Database Name]
SELECT COLUMN_NAME,*
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'YourTableName' AND TABLE_SCHEMA='YourSchemaName'
This is the easiest way
exec sp_columns [tablename]
Something like this?
sp_columns @table_name=your table name
One method is to query syscolumns:
select
syscolumns.name as [Column],
syscolumns.xusertype as [Type],
sysobjects.xtype as [Objtype]
from
sysobjects
inner join
syscolumns on sysobjects.id = syscolumns.id
where sysobjects.xtype = 'u'
and sysobjects.name = 'MyTableName'
order by syscolumns.name
try this
select * from <tablename> where 1=2
...............................................
This seems a bit easier then the above suggestions because it uses the OBJECT_ID() function to locate the table's id. Any column with that id is part of the table.
SELECT *
FROM syscolumns
WHERE id=OBJECT_ID('YOUR_TABLE')
I commonly use a similar query to see if a column I know is part of a newer version is present. It is the same query with the addition of {AND name='YOUR_COLUMN'} to the where clause.
IF EXISTS (
SELECT *
FROM syscolumns
WHERE id=OBJECT_ID('YOUR_TABLE')
AND name='YOUR_COLUMN'
)
BEGIN
PRINT 'Column found'
END
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With