tags:

views:

769

answers:

2

Hi friends,

It's possible to find the number of rows in a table:
select count(*) from tablename

Is it possible to find the number of columns in a table?

Thanks in Advance.
Praveen J

+14  A: 
SELECT COUNT(*)
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_name = 'tbl_name'
Nathan Koop
Hi i cant under stand, here the information can you please give me the query once again.. table name = postcolumns = PostingID, PostingDate, Body
praveenjayapal
Unless I am confused, you are looking for how many columns are in the table. If you execute the following query, it will return 3 (assuming the only columns are PostingId, PostingDate and Body)SELECT COUNT(*)FROM INFORMATION_SCHEMA.COLUMNSWHERE table_name = 'post'
Nathan Koop
+1  A: 

Or use the sys.columns

--SQL 2005
SELECT  *
FROM    sys.columns
WHERE   OBJECT_NAME(object_id) = 'spt_values'
-- returns 6 rows = 6 columns

--SQL 2000
SELECT  *
FROM    syscolumns
WHERE   OBJECT_NAME(id) = 'spt_values'
-- returns 6 rows = 6 columns

SELECT  *
FROM    dbo.spt_values
    -- 6 columns indeed
jerryhung