Quantcast
Channel: SQL Server
Viewing all articles
Browse latest Browse all 3819

Wiki Page: Calculate Current Table Sizes

$
0
0
See Also: [[wiki:Main Page|Main_Page]] - [[wiki:Transact SQL Code Library|Transact SQL Code Library]] - [[wiki:Size Calculations|Size Calculations]] This script loops through all of the objects in the current database that are user tables, and runs sp_SpaceUsed for each of them. It returns a list of all objects in order of table name, but you can change the sort order to go by size instead. T-SQL Code If you get an error on a case-sensitive server saying that it can't find SP_SPACEUSED, it's due to the rendering engine we use on SQLServerPedia. Change that part of the code to read sp_spaceused instead, lower case. DECLARE @CurrentTableName NVARCHAR(100) CREATE TABLE #TableList (TableName NVARCHAR(100) , RowsCount INT , KBReserved VARCHAR(15) , KBData VARCHAR(15) , KBIndex VARCHAR(15) , KBUnused VARCHAR(15) ) -- Run a cursor through all of the tables DECLARE result_cursor CURSOR FOR SELECT '[' + name + ']' FROM dbo.sysobjects WHERE type = 'U' OPEN result_cursor FETCH NEXT FROM result_cursor INTO @CurrentTableName WHILE @@FETCH_STATUS = 0 BEGIN INSERT INTO #TableList EXEC dbo.sp_spaceused @CurrentTableName FETCH NEXT FROM result_cursor INTO @CurrentTableName END --end loop --clean up CLOSE result_cursor DEALLOCATE result_cursor UPDATE #TableList SET KBReserved = REPLACE(KBReserved, ' KB', ) , KBData = REPLACE(KBData, ' KB', ) , KBIndex = REPLACE(KBIndex, ' KB', ) , KBUnused = REPLACE(KBUnused, ' KB', ) SELECT * FROM #TableList ORDER BY TableName DROP TABLE #TableList Query Test Checklist Works on SQL Server 2008: Yes Works on SQL Server 2005: Yes Works on SQL Server 2000: No (but this could be rewritten to work on SQL 2000) Works on Standard Edition: Yes Works on case-sensitive servers: Yes (see note above query)

Viewing all articles
Browse latest Browse all 3819

Trending Articles