T-SQL 如何获取一个表的列名

时间:2022-12-18 22:11:49

方法1:

exec sp_columns [{table_name}],[{schema_name}]

方法2:

SELECT *

FROM syscolumns

WHERE id=OBJECT_ID(‘{schema_name}.{table_name}]')

方法3:

SELECT COLUMN_NAME,*

FROM INFORMATION_SCHEMA.COLUMNS

WHERE TABLE_NAME = '{table_name}' AND TABLE_SCHEMA='{schema_name}'

方法4: 

select

   syscolumns.name as [Column],*

from

   sys.objects as sysobjects, sys.columns as  syscolumns

where sysobjects.object_id = syscolumns.object_id

and   sysobjects.schema_id = schema_id('{schema_name}')

and   sysobjects.name = '{table_name}'

order by syscolumns.name