How to determine table data sizes via T-SQL

Written by Brett Veenstra

I needed a quick and dirty way to find out how much data tables were consuming in a SQL Server database. Ripping off what sp_spaceused does, I came up with this:


SELECT
    t.name,
    LTRIM (STR (
        SUM (
        CASE
        WHEN (index_id < 2) THEN (in_row_data_page_count + lob_used_page_count + row_overflow_used_page_count)
        ELSE lob_used_page_count + row_overflow_used_page_count
        END
    )  * 8, 15, 0) + ' KB') AS DataSize
FROM sys.dm_db_partition_stats s
INNER JOIN sys.tables t ON t.object_id = s.object_id
GROUP BY t.name