Tuesday, October 3, 2006

SQL Server: How to list the columns in a table

This is a simple script to list the columns in a given table in SQL Server. It uses the SYSCOLUMNS, SYSTYPES, and SYSOBJECTS system tables:




use mydatabase

select left(col.name, 32) col_name, left(typ.name, 32) col_type, typ.length
from syscolumns col
left join sysobjects tab on tab.id = col.id
left join systypes typ on typ.xtype = col.xtype
where tab.name = 'TABLE NAME' and tab.xtype = 'U'

go

No comments:

Post a Comment

I moderate comments blog posts over 14 days old. This keeps a lot of spam away. I generally am all right about moderating. Thanks for understanding.