Saturday December 03, 2011 at 17:51

T-SQL Table Column Lookup Query

Here is a quick snippet of T-SQL that I use to find the column names in a given table in SQL Server:

SELECT ORDINAL_POSITION ,COLUMN_NAME ,DATA_TYPE ,CHARACTER_MAXIMUM_LENGTH ,IS_NULLABLE ,COLUMN_DEFAULT FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = ‘table_name’ ORDER BY ORDINAL_POSITION ASC;

Just replace ‘table_name’ with the name of the table you want to get information about.  This is extremely useful especially when the list of columns is large and I need to access data on a field by field basis for transformation, comparison, etc…