How to view the size of all tables in SQL Server?

To view the size of all tables in SQL Server, you can use the following query statement:

SELECT 
    t.NAME AS TableName,
    s.NAME AS SchemaName,
    p.rows AS RowCounts,
    SUM(a.total_pages) * 8 AS TotalSizeKB 
FROM 
    sys.tables t 
INNER JOIN 
    sys.indexes i ON t.OBJECT_ID = i.object_id 
INNER JOIN 
    sys.partitions p ON i.object_id = p.OBJECT_ID AND i.index_id = p.index_id 
INNER JOIN 
    sys.allocation_units a ON p.partition_id = a.container_id 
LEFT OUTER JOIN 
    sys.schemas s ON t.schema_id = s.schema_id 
GROUP BY 
    t.Name, s.Name, p.Rows 
ORDER BY 
    TotalSizeKB DESC;

This will return a result set containing the names, schemas, number of rows, and sizes (in kilobytes) of all tables.

Leave a Reply 0

Your email address will not be published. Required fields are marked *


广告
Closing in 10 seconds
bannerAds