Click here to Skip to main content
15,996,586 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,

I have to find number of column from table and name of each column from that table.
please help me,how to find it?
Thanks.
Posted

This is to find the number of columns in a table.
SQL
SELECT COUNT(*) AS num_of_columns
FROM INFORMATION_SCHEMA.COLUMNS a
WHERE a.TABLE_NAME = 'tablename'

This is to get all the columns in a table.
SQL
SELECT COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS a
WHERE a.TABLE_NAME = 'tablename'
 
Share this answer
 
v3
Comments
Abhinav S 3-Sep-11 0:25am    
Nice and precise. 5.
Try this SQL:
SQL
EXEC SP_Columns myTable
It will return a table with each column in a separate row. This gives you all the info you need!
 
Share this answer
 
Comments
Abhinav S 3-Sep-11 0:26am    
Good alternative.
This query will do it for you:
SQL
SELECT * FROM sys.columns WHERE object_id = OBJECT_ID('dbo.Table1')
 
Share this answer
 
Comments
RaviRanjanKr 2-Sep-11 15:57pm    
Nice Answer, its new to me, have a my 5+
Check this out[^]. A slight modification to the query will give you a list of all columns.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900