SQL SERVER – Query to Find ByteSize of All the Tables in Database

SELECT CASE WHEN (GROUPING(sob.name)=1THEN 'All_Tables'
   
ELSE ISNULL(sob.name'unknown'END AS Table_name,
   
SUM(sys.lengthAS Byte_Length
FROM sysobjects sobsyscolumns sys
WHERE sob.xtype='u' AND sys.id=sob.id
GROUP BY sob.name
WITH CUBE

Reference : Pinal Dave (http://blog.SQLAuthority.com)

About these ads

13 thoughts on “SQL SERVER – Query to Find ByteSize of All the Tables in Database

  1. @Sandhu

    You can find it through two ways

    a) Query Analyzer.
    b) SSMS/Enterprise Manager.

    a). Query Analyzer :

    1. Sp_helpdb — execute this is master database.
    Output : you will get information about all databases sizes.
    2. sp_help ”database_name” —
    Output: gives in detail information about Log and data file, with their sizes.
    2. Sp_helpfile ( execute in the database for which you want size). Will give the same information about log and data file of the current database.

    b) Enterprize manager: Expand Sql Server – Expand databases- click on database name = right click on database name – view – all task and then you can see in detail information about the size of the log file and also data file. And also you can see how much is filled, how much is empty.
    b) in Enterprise Manager – right click database name , click properties, you will see the database size.
    c) SSMS (2005) : Right click database select reports- click standard reports – Disk usage. you will see a report and you can see all details about sizes. If you expand the last point ” Disk usage by datafiles” you can see some good information.
    d) SSMS (2005) – Right click database name, click properties and you can see database size.

    To see the only logspace for all databases. Execute this statement,

    DBCC SQLPERF( logspace)

    Output: Will give you detail information about Logfile of the database.

    your second question about tables:

    Please visit this link : http://blog.sqlauthority.com/2008/07/08/sql-server-find-space-used-for-any-particular-table/

    Hope this helps.
    Thanks
    Imran.

  2. Hi Pinal,
    My table structure is like this.
    using with above script am getting the Max_Length as “3″

    Column_name Type Computed Length
    ID int no 4
    SPName varchar no -1

    But varchar(max) length is 8000bytes …
    How can we get exact lenght. Correct me if am wrong…

    Regards,
    abhIShek BandI

  3. Pingback: SQL SERVER – Beginning New Weekly Series – Memory Lane – #002 « SQL Server Journey with SQL Authority

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s