Ultimate Collection - { fslBlog & faisalmb.com } Ultimate Collection - { fslBlog & faisalmb.com }   
Blog   |   Site   |   Posts (347)   |   Tags Xplorer   |   Feed Subscribe Free! Aha! you surfing post Find rowcount, columncoun... Sign in    Partner Site - Real Home Contact Search   

Fri

20

Jun

2008

Fri-20-06-2008
   

Find rowcount, columncount, table size in Sql Server Database



Today I tried to find out which table is taking much space in DB or to find the numbers of columns and rows in all tables of DB

Following query will return the required result

=========

USE DatabaseName
GO
CREATE TABLE #temp (
                table_name sysname ,
                row_count INT,
                reserved_size VARCHAR(50),
                data_size VARCHAR(50),
                index_size VARCHAR(50),
                unused_size VARCHAR(50))
    SET NOCOUNT ON
INSERT     #temp
    EXEC       sp_msforeachtable 'sp_spaceused "?"'
SELECT     a.table_name,
            a.row_count,
            COUNT(*) AS col_count,
            a.data_size
    FROM       #temp a
            INNER JOIN information_schema.columns b
            ON a.table_name collate database_default
    = b.table_name collate database_default
    GROUP BY   a.table_name, a.row_count, a.data_size
    ORDER BY   CAST(REPLACE(a.data_size, 'KB', '') AS integer) DESC
DROP TABLE #temp

 

 

 


Comments

Add Comment Add comment

 
 
 
   Country flag

Click to change image  --> 

biuquote
  • Comment
  • Preview
Loading





Intro

Faisal Bashir
Consultant / Software Architect
KalSoft Limited
Microsoft Certified Technology Specialist.
Currently in Dubai. [more]

Right Now

How could u reach the pearl by only looking at the sea? if u seek the pearl, be a diver: the diver needs several qualities, he must trust his rope and his life to the Friend's hand, he must stop breating and he must jump - Jalaluddin Rumi.

Recent Comments

Comment RSS

Calendar

<<  February 2012  >>
MoTuWeThFrSaSu
303112345
6789101112
13141516171819
20212223242526
2728291234
567891011

View posts in large calendar

Then which of the favours of your Lord will you twain deny? (Holy Quraan - 55-21)
383338 hits. (Best viewed @ 1024x768 resolution min.) Comments here...
© 2001-2011 Muhammad Faisal | Disclaimer | Contact | Partner Site