Search This Blog

Showing posts with label SQL Server 2012. Show all posts
Showing posts with label SQL Server 2012. Show all posts

Thursday, 24 April 2014

SQL Server physical table storage sizes

Quick a script that shows physical storage usage for tables.  Copied from http://mitchelsellers.com/blogs/2007/07/27/determing-sql-server-table-size.aspx


CREATE TABLE #TempTableNik (
tableName varchar(100),     
numberofRows varchar(100),     
reservedSize varchar(50),     
dataSize varchar(50),     
indexSize varchar(50),     
unusedSize varchar(50) 


DECLARE @TableName VARCHAR(100)

DECLARE tableCursor CURSOR FOR select [name] from dbo.sysobjects  where  OBJECTPROPERTY(id, N'IsUserTable') = 1 FOR READ ONLY
OPEN tableCursor

FETCH NEXT FROM tableCursor INTO @TableName

--Loop until the cursor was not able to fetch
WHILE (@@Fetch_Status >= 0)
BEGIN
    --Dump the results of the sp_spaceused query to the temp table
    INSERT  #TempTableNik
        EXEC sp_spaceused @TableName

    --Get the next table name
    FETCH NEXT FROM tableCursor INTO @TableName
END

--Get rid of the cursor
CLOSE tableCursor
DEALLOCATE tableCursor

--Select all records so we can use the reults
SELECT *
FROM #TempTableNik
order by dataSize desc, tableName

--Final cleanup!
DROP TABLE #TempTableNik