Search This Blog

Friday, March 8, 2013

Find large tables size in SQL Server

SQL Server > Scripts

Microsoft SQL Server has two undocumented stored procedures that allow you to process through all tables in a database (sp_MSforeachtable), or all databases (sp_MSforeachdb).

sp_MSforeachtable

Example: Find tables size in SQL Server

SET NOCOUNT ON
CREATE TABLE #tbl_size
(
       tbl_name            sysname ,
       rows int,
       reserved_size VARCHAR(50),
       data_size           VARCHAR(50),
       index_size          VARCHAR(50),
       unused_size         VARCHAR(50)
)
INSERT #tbl_size
EXEC sp_msforeachtable 'sp_spaceused ''?'''
select
       *
from
       #tbl_size
order by
       CAST(REPLACE(data_size, ' KB', '') AS int) desc

drop table #tbl_size