SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Print table name and count the number of columns
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

goodman2253
Yak Posting Veteran

83 Posts

Posted - 05/18/2012 :  03:05:24  Show Profile  Reply with Quote
I want to set up the loop which should print the
table name and the count of number of columns present in
that table

It should something like

for i in all tables
{
select i, no of fields
from i
}
in sql server 2005

And the output wanted is

Table1--3
Table2--4

khtan
In (Som, Ni, Yak)

Singapore
16745 Posts

Posted - 05/18/2012 :  03:27:08  Show Profile  Reply with Quote

select	object_name(object_id), count(*)
from	sys.columns
group by object_name(object_id)



KH
Time is always against us

Go to Top of Page

Lumbago
Norsk Yak Master

Norway
3241 Posts

Posted - 05/18/2012 :  04:03:23  Show Profile  Reply with Quote
SELECT TABLE_NAME, COUNT(*)
FROM INFORMATION_SCHEMA.COLUMNS
GROUP BY TABLE_NAME
ORDER BY TABLE_NAME

- Lumbago
My blog-> http://thefirstsql.com/2011/07/08/how-to-find-gaps-in-identity-columns-at-the-speed-of-light/

Edited by - Lumbago on 05/18/2012 04:03:40
Go to Top of Page

goodman2253
Yak Posting Veteran

83 Posts

Posted - 05/18/2012 :  04:47:52  Show Profile  Reply with Quote
The number of rows also wanted with all that stuff.

The final output wanted is

Table_Name--count_no_of_rows--count_no_of_cloumns

T1--200--12
T2--800--11
Go to Top of Page

goodman2253
Yak Posting Veteran

83 Posts

Posted - 05/18/2012 :  05:30:12  Show Profile  Reply with Quote
When using the table
sys.dm_db_partition_stats st

It gives an error, the error is
Msg 297, Level 16, State 1, Line 1
The user does not have permission to perform this action.
Go to Top of Page

khtan
In (Som, Ni, Yak)

Singapore
16745 Posts

Posted - 05/18/2012 :  05:46:39  Show Profile  Reply with Quote
refer to BOL http://msdn.microsoft.com/en-us/library/ms187737.aspx
quote:
Requires VIEW DATABASE STATE permission to query the sys.dm_db_partition_stats dynamic management view



KH
Time is always against us


Edited by - khtan on 05/18/2012 06:39:41
Go to Top of Page

goodman2253
Yak Posting Veteran

83 Posts

Posted - 05/18/2012 :  06:07:31  Show Profile  Reply with Quote
Referred but didn't get the clear picture.
Go to Top of Page

ashishashish
Constraint Violating Yak Guru

India
404 Posts

Posted - 05/18/2012 :  06:46:59  Show Profile  Send ashishashish a Yahoo! Message  Reply with Quote
I hope you want it like that.. and hope it will work

SELECT [Rows].TableName,
[Rows].NoOfRows,
Count([Columns].column_id) NoOfColumns
FROM sys.columns [Columns]
INNER JOIN (SELECT st.Name AS TableName,
Sum(CASE
WHEN ( p.index_id < 2 )
AND ( a.type = 1 ) THEN p.rows
ELSE 0
END) AS NoOfRows
FROM sys.partitions p
INNER JOIN sys.allocation_units a
ON p.partition_id = a.container_id
INNER JOIN sys.tables st
ON st.object_id = p.Object_ID
INNER JOIN sys.schemas sch
ON sch.schema_id = st.schema_id
GROUP BY st.name) [Rows]
ON Object_name([Columns].object_id) = [Rows].TableName
GROUP BY [Rows].TableName,
[Rows].NoOfRows


------------------------------------------------
The answer is always no till than you don't ask.
Go to Top of Page

goodman2253
Yak Posting Veteran

83 Posts

Posted - 05/18/2012 :  06:47:21  Show Profile  Reply with Quote
Is their any different solution we can have
Go to Top of Page

ashishashish
Constraint Violating Yak Guru

India
404 Posts

Posted - 05/18/2012 :  06:52:56  Show Profile  Send ashishashish a Yahoo! Message  Reply with Quote
Is this solution having some issues..??

------------------------------------------------
The answer is always no till than you don't ask.
Go to Top of Page

goodman2253
Yak Posting Veteran

83 Posts

Posted - 05/18/2012 :  06:53:05  Show Profile  Reply with Quote
Thanks ashishashish..
It really works
Go to Top of Page

ashishashish
Constraint Violating Yak Guru

India
404 Posts

Posted - 05/18/2012 :  06:54:34  Show Profile  Send ashishashish a Yahoo! Message  Reply with Quote
Your welcome.

------------------------------------------------
The answer is always no till than you don't ask.
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.16 seconds. Powered By: Snitz Forums 2000