ASP Snippets

Categories






Alerts

Free Alerts

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




Follow us on twitter.




Zoom In | Zoom Out


Author is awarded Most Valuable Professional award by Microsoft ASP/ASP.Net

Tip – Query to get all column names from database table in SQL Server.

Author:Mudassar Khan

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

 

Posted: May 09 2009, 07:35 by Mudassar Khan | Comments (1) RSS comment feed |
Filed under: SQL Server | Tips

Views: 4922
Page copy protected against web site content infringement by Copyscape


If you like this article, help us grow by bookmarking this page on any social bookmarking site.
Bookmark and Share





Comments









Community News





Web Hosting SpotLight


Consulting


For consulting and work related queries click here.



Advertise


Advertise with us. For more details click here.


Suggestions


Please provide your valuable suggesstions here.

This Site is hosted on

Lunarpages.com Web Hosting