Friday, October 9, 2009

How to get the Column information for a Table using T-SQL

in this sample I am getting the column information for a table called ‘Account’:

SELECT     Columns.name AS ColumnName, ColumnTypes.name AS Type, Columns.prec AS Precision, Columns.scale AS Scale, 
Columns.isnullable AS IsNullable
FROM sys.sysobjects AS Tables INNER JOIN
sys.syscolumns AS Columns ON Columns.id = Tables.id INNER JOIN
sys.systypes AS ColumnTypes ON Columns.xtype = ColumnTypes.xtype
WHERE (Tables.type = 'U') AND (Tables.name = N'Account')
ORDER BY ColumnName