Thursday, August 11, 2011

Find column names in a table

Thought this might save sometime going through Books Online to find this; There are few ways to do this in SQL Server 2005 onwards.

Useing Information_Schema.Columns is the best out of those. This is because it returns results in a readable format. Below is how you can do this;

SELECT * FROM information_schema.columns  WHERE Table_Name = 'YourTableName' 

Alternately you can use SP_Columns as well, usage is as below;

EXEC sp_Columns 'YourTableName'

In addition to this if you want to say get all the columns in a database which has a particular name or part of the name in it, this can be also achieved using a script like this;

SELECT * FROM information_schema.columns
WHERE Column_Name LIKE  '%url%'

This will return all columns which are having url as part of the name.

No comments:

Post a Comment