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
Quote Reply
Re: [sbaser] Count columns in a table In reply to
You can use the global below:

sub {
my @cols = $DB->table('table_name')->ordered_columns;
return scalar @cols;
}

TheStone.

B.