Monday, February 09, 2015

System Views for Column Names


SELECT 

  ORDINAL_POSITION 
  ,COLUMN_NAME 
  ,DATA_TYPE 
  ,CHARACTER_MAXIMUM_LENGTH 
  ,IS_NULLABLE 
  ,COLUMN_DEFAULT
FROM 
  INFORMATION_SCHEMA.COLUMNS 
WHERE 
  TABLE_NAME = 'Customer' 
ORDER BY 
  ORDINAL_POSITION ASC; 

You can do something like the following to give you a comma separated list that can then be used for a query. 

SELECT 

  COLUMN_NAME + ',' 
FROM 
  INFORMATION_SCHEMA.COLUMNS 
WHERE 
  TABLE_NAME = 'Customer' 
ORDER BY 
  ORDINAL_POSITION ASC; 

No comments:

Post a Comment