SQLUSA.com
SQL SERVER 2008 GRAND SLAM
FREE TRIAL  CLICK HERE TO ORDER  SEARCH
SQL JOBS SQL Server Training Scripts JOB BANK
  SQL Server 2008 Training Scripts  
  SQL Server 2005 Training Scripts  
News SQL Server Articles SQL Format

How to list dynamically space used by tables?

Execute the following Microsoft SQL Server T-SQL script in SSMS Query Editor to compile the dynamic space used stored procedure:

USE AdventureWorks2008;

 

GO

 

CREATE PROC dm_spaceused

AS

  BEGIN

    WITH cteSpaceUsage([Schema],[Table],[Index],Used,Reserved,IndexRows,TableRows)

         AS (SELECT s.Name,

                    o.Name,

                    coalesce(i.Name,'HEAP'),

                    p.used_page_count * 8,

                    p.reserved_page_count * 8,

                    p.row_count,

                    CASE

                      WHEN i.index_id IN (0,1) THEN p.row_count

                      ELSE 0

                    END

             FROM   sys.dm_db_partition_stats p

                    JOIN sys.objects AS o

                      ON o.object_id = p.object_id

                    JOIN sys.schemas AS s

                      ON s.schema_id = o.schema_id

                    LEFT OUTER JOIN sys.indexes AS i

                      ON i.object_id = p.object_id

                         AND i.index_id = p.index_id

             WHERE  o.type_desc = 'USER_TABLE'

                    AND o.is_ms_shipped = 0)

    SELECT   [Schema] = coalesce(t.[Schema],'Grand Total'),

             [Table] = coalesce(t.[Table],'Total'),

             [Index] = coalesce(t.[Index],'Sub Total'),

             CASE grouping(t.[Index])

               WHEN 0 THEN sum(t.IndexRows)

               ELSE sum(t.TableRows)

             END AS [RowCount],

             sum(t.Used)     AS [Used(KB)],

             sum(t.Reserved) AS [Reserved(KB)]

    FROM     cteSpaceUsage AS t

    GROUP BY t.[Schema],

             t.[Table],

             t.[Index] WITH ROLLUP

    ORDER BY grouping(t.[Schema]),

             t.[Schema],

             grouping(t.[Table]),

             t.[Table],

             grouping(t.[Index]),

             t.[Index]

  END

 

GO

 

EXEC dm_spaceused

Related article:

Script to analyze table space usage

 

Order SQL 2008 GRAND SLAM Today!
SQLUSA.com Home Page
SQL Server Training at www.sqlusa.com.
SQL Server 2008 Video Training at www.sqlusa.com.
SQL Server 2005 Training Videos at www.sqlusa.com.
Microsoft SQL Server 2000 Training Videos at www.sqlusa.com.

FREE SQL & Business Intelligence / OLAP Short Videos on YOUTUBE.com

Microsoft Community Contributor 2011
Invest in Your SUCCESS!

Search SQLUSA FREE SQL Server Articles & FREE T-SQL Scripts


Copyright 2005-2011, SMI Corp. All Rights Reserved.

SQL Server 2012 is a program product of Microsoft Corporation.
SQL Server 2008 is a program product of Microsoft Corporation.
SQL Server 2005 is a program product of Microsoft Corporation.
SQL Server 2000 is a program product of Microsoft Corporation.