ASPSnippets

Alerts
Get notified when a new article is published.

Name
 
Email

Your email will always be private and will not be shared.

Follow us on twitter.
 
Tip Query to get all column names from database table in SQL Server
Author Name: Mudassar Khan Published Date: May 09, 2009
Filed Under :
SQL Server
Views: 5032

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

 


If you like this article, help us grow by bookmarking this page on any social bookmarking site.
Bookmark and Share Page copy protected against web site content infringement by Copyscape

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.
There is no need to add BR tags. Simply press enter for new line

Name*  
Email*
Comment*  
Security code
Security code