Tip Query to get all column names from database table in SQL Server
 
Author:
Filed Under: SQL Server
Published Date: May 09, 2009
Views: 6439
 

Abstract: Here Mudassar Ahmed Khan has explained How to retrieve column names from a database table using SQL Query in SQL Server Database

Comments:  1

 

Many times there’s a requirement to get all columns for a particular table in database. Hence here is a SQL Query that does that.

 

SELECT COLUMN_NAME

FROM INFORMATION_SCHEMA.COLUMNS

WHERE TABLE_NAME = 'Your Table Name'

ORDER BY ORDINAL_POSITION

 

For example if I want to get all columns in Employees table of NorthWind database then it would be

SELECT COLUMN_NAME

FROM INFORMATION_SCHEMA.COLUMNS

WHERE TABLE_NAME = 'Employees'

ORDER BY ORDINAL_POSITION

 

There’s also a System Stored Procedure sp_columns that also does the above but with some additional information

EXEC sp_columns 'Your Table Name'

 

If I want to get all columns in Employees table of NorthWind database then it would be

EXEC sp_columns 'Employees'

 

Try it your self

 









Related Articles



Comments



Add comments

You can add your comment about this article using the form below. Make sure you provide a valid email address
else you won't be notified when the author replies to your comment

Please note that all comments are moderated and will be deleted if they are
  • Not relavant to the article
  • Spam
  • Advertising campaigns or links to other sites
  • Abusive content.
Please do not post code, scripts or snippets.

Name*: Required
Email*: Required
Comment*: Required
Security code*: CaptchaInvalid Security Code
  Submit