Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 how to find no. of columns ?????

Author  Topic 

SanjaySutar
Starting Member

18 Posts

Posted - 2008-05-05 : 01:01:34
Hi is there any way to find no. of columns in a database table
like we can do that in Oracle by typing DESC tablename

San

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-05-05 : 01:15:03
SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'YourTable'
Go to Top of Page

vivekroberts
Starting Member

2 Posts

Posted - 2008-05-05 : 02:23:22
select count(*)
from syscolumns
where id = ( select id from sysobjects where name = 'Your Table')

Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-05-08 : 04:29:50
quote:
Originally posted by vivekroberts

select count(*)
from syscolumns
where id = ( select id from sysobjects where name = 'Your Table' and xtype='u')

or

select count(*)
from syscolumns
where id = object_id('Your Table')






Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

Anoop
Starting Member

6 Posts

Posted - 2008-05-08 : 04:46:36
instead of syscolumns use sys.columns if using SQL Server 2005.

select count(*)
from sys.columns
where object_id = ( select object_id from sys.tables where name = 'Your Table')
Go to Top of Page

eralper
Yak Posting Veteran

66 Posts

Posted - 2008-05-08 : 07:03:34
Hi,
We can go further by using Object_ID() function as follows

select count(*) from sys.columns where object_id = object_id('table_name')





-------------
Eralper
http://www.kodyaz.com
Go to Top of Page
   

- Advertisement -