| Author |
Topic |
|
goodman2253
Yak Posting Veteran
88 Posts |
Posted - 2012-05-18 : 03:05:24
|
| I want to set up the loop which should print the table name and the count of number of columns present in that tableIt should something likefor i in all tables{select i, no of fieldsfrom i}in sql server 2005And the output wanted isTable1--3Table2--4 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2012-05-18 : 03:27:08
|
[code]select object_name(object_id), count(*)from sys.columnsgroup by object_name(object_id)[/code] KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
Lumbago
Norsk Yak Master
3271 Posts |
Posted - 2012-05-18 : 04:03:23
|
| SELECT TABLE_NAME, COUNT(*)FROM INFORMATION_SCHEMA.COLUMNSGROUP BY TABLE_NAMEORDER BY TABLE_NAME- LumbagoMy blog-> http://thefirstsql.com/2011/07/08/how-to-find-gaps-in-identity-columns-at-the-speed-of-light/ |
 |
|
|
goodman2253
Yak Posting Veteran
88 Posts |
Posted - 2012-05-18 : 04:47:52
|
| The number of rows also wanted with all that stuff.The final output wanted is Table_Name--count_no_of_rows--count_no_of_cloumnsT1--200--12T2--800--11 |
 |
|
|
goodman2253
Yak Posting Veteran
88 Posts |
Posted - 2012-05-18 : 05:30:12
|
| When using the tablesys.dm_db_partition_stats stIt gives an error, the error is Msg 297, Level 16, State 1, Line 1The user does not have permission to perform this action. |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
|
|
goodman2253
Yak Posting Veteran
88 Posts |
Posted - 2012-05-18 : 06:07:31
|
| Referred but didn't get the clear picture. |
 |
|
|
ashishashish
Constraint Violating Yak Guru
408 Posts |
Posted - 2012-05-18 : 06:46:59
|
| I hope you want it like that.. and hope it will workSELECT [Rows].TableName, [Rows].NoOfRows, Count([Columns].column_id) NoOfColumnsFROM 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].TableNameGROUP BY [Rows].TableName, [Rows].NoOfRows ------------------------------------------------The answer is always no till than you don't ask. |
 |
|
|
goodman2253
Yak Posting Veteran
88 Posts |
Posted - 2012-05-18 : 06:47:21
|
| Is their any different solution we can have |
 |
|
|
ashishashish
Constraint Violating Yak Guru
408 Posts |
Posted - 2012-05-18 : 06:52:56
|
| Is this solution having some issues..??------------------------------------------------The answer is always no till than you don't ask. |
 |
|
|
goodman2253
Yak Posting Veteran
88 Posts |
Posted - 2012-05-18 : 06:53:05
|
| Thanks ashishashish..It really works |
 |
|
|
ashishashish
Constraint Violating Yak Guru
408 Posts |
Posted - 2012-05-18 : 06:54:34
|
| Your welcome.------------------------------------------------The answer is always no till than you don't ask. |
 |
|
|
|