Here is a query to find the ByteSize of all the tables in a database.

SELECT CASE WHEN (GROUPING(sob.name)=1) THEN 'All_Tables' ELSE ISNULL(sob.name, 'unknown') END AS Table_name, SUM(sys.length) AS Byte_Length FROM sysobjects sob, syscolumns sys WHERE sob.xtype='u' AND sys.id=sob.id GROUP BY sob.name WITH CUBE
What the ByteSize of All the Tables Really Tells You
Before you use this number, it helps to know what it measures. The query adds up the length column from syscolumns for every column of every user table. That length is the defined width of each column in bytes, so the total is the largest size one row can have as declared. It is not the space the table uses on disk, and it does not change when you add or delete rows.
That still makes it useful. A wide row means fewer rows fit on each 8 KB data page, and more pages mean more reads for every scan. When you review a design, look at the widest tables first and ask if every column needs its current size. A CHAR(500) column that usually holds 20 characters is a common find.
A few things to keep in mind when you read the output:
NVARCHARandNCHARcolumns use two bytes per character, so their length is double the number you see in the data type.- On SQL Server 2005 and later,
VARCHAR(MAX),NVARCHAR(MAX)andVARBINARY(MAX)columns show a length of -1, which pulls the total down instead of up. - The row labeled All_Tables comes from
WITH CUBE. It is the grand total for the database, not a real table.
If you need the real space each table uses, reach for a different tool. sp_spaceused with a table name shows rows, reserved space and data space. On SQL Server 2005 and later, sys.dm_db_partition_stats gives row counts and page counts for every table in one query, and the Disk Usage by Top Tables report in Management Studio shows the same idea without typing anything.
One more tip. sysobjects and syscolumns are kept for backward compatibility. On newer versions I write the same idea with sys.tables and sys.columns, and I group by the schema name as well, so two tables with the same name in different schemas are not added together.
Published by Pinal Dave on SQLAuthority. More of my work at pinaldave.com.





15 Comments. Leave new
how i can find? please reply
1.What is the current file system size of the database?
2. What is the current size of the tables (used)?
@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.
Good article Thank you :)
Simple & sweet!
great stuff…thanks
i am the beginner.. so may i know what is byte_length…
select a.name,Sum(b.[max_length]) from sys.objects a INNER JOIN
sys.columns b ON a.object_id=b.object_id Where type = ‘u’ Group by a.name
select a.name,Sum(b.[max_length]) from sys.objects a INNER JOIN
sys.columns b ON a.object_id=b.object_id Where type = ‘u’ Group by a.name
very helping article thanks buddy
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
Nice thank you for such a nice information
Nice piece of information. As i am beginner as a DBA . I have started referring all your blogs from the beginning.
Select ISNULL(t.name,’AllTables’)[TableName],Sum(C.max_length)[TableSize]
from sys.tables T
join sys.columns C on t.object_id=C.object_id
Group by rollup (t.name)
Select ISNULL(t.name,’AllTables’)[TableName],Sum(C.max_length)[TableSize]
from sys.tables T
join sys.columns C on t.object_id=C.object_id
Group by Grouping Sets (t.name)
—-for DBs having compatablilty Mode less than 100 ;)
Good One.