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

Fri

20

Jun

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

 

 

 

Rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: ,

Categories : Computers / Programming / Mobiles



Add comment

 Your Name*  
 Your E-mail (will not get publish)  
 Your Website  
 Your Country   Country flag

Comment*   
[b][/b] - [i][/i] - [u][/u]- [quote][/quote]








Intro

Name of author Faisal Bashir
Senior Software Engineer
KalSoft Limited
Microsoft Certified Technology Specialist.

Add to/Bookmark with:


deliciousDelicious  diggDigg  redditreddit  facebookFacebook  stumbleuponStumbleUpon

Add to Google Reader or Homepage Add to My AOL
Add to My Yahoo!

Recent comments


When you enter the toilet you should say, In the Name of Allah, I seek protection in You from unclean spirits, male and female. (Muhammad - peace be upon him - sallallaho alaihi wassallam) (ref. Abu Dawud 4/264, Ahmad 2/389 and Fath Al-Bari 1/254)
79773 hits. (Best viewed @ 1024x768 resolution min.) Comments here...
© 2001-2008 Muhammad Faisal | Disclaimer | Contact | Partner Site