Gossamer Forum
Home : Products : DBMan SQL : Discussion :

Count columns in a table

Quote Reply
Count columns in a table
Hello,

I want to count how many columns a table has, and to display their name list.

Example:
Table name is tbl_mytable,
Assume that tbl_mytable has 5 columns named mycol1,...,mycol5.

What is the SQL command to count and to show the list?

output should be:

There are 5 columns tbl_mytable has

Column Names
mycol1
mycol2
mycol3
mycol4
mycol5


The command:
SELECT COL_NAME(OBJECT_ID('tbl_mytable'), 1)
returns first column name: mycol1

If I can count the columns, it is easy to get the list with like
for x = 1 to columncount

SELECT COL_NAME(OBJECT_ID('tbl_mytable'), columncount)

next

Regards,
Sbaser
Subject Author Views Date
Thread Count columns in a table sbaser 3439 Dec 8, 2005, 5:14 AM
Post Re: [sbaser] Count columns in a table
604 3324 Dec 9, 2005, 9:56 AM