-- Can't remember where I downloaded it from originally
CREATE
PROCEDURE GetAllTableSizes
AS
AS
DBCC UPDATEUSAGE (0)
DECLARE @TableName VARCHAR(100) --For storing values in the cursor
--Cursor to get the name of all user tables from the sysobjects listing
DECLARE
tableCursor CURSOR FOR
SELECT [name] FROM dbo.sysobjects WHERE OBJECTPROPERTY(id, N'IsUserTable') = 1 FOR READ ONLY
--A procedure level temp table to store the results
CREATE TABLE #TempTable(
tableName varchar(100),
numberofRows varchar(100),
reservedSize varchar(50),
dataSize varchar(50),
indexSize varchar(50),
unusedSize varchar(50)
)
OPEN
tableCursor
FETCH NEXT FROM tableCursor
INTO @TableName
WHILE
(@@Fetch_Status >=
0)
BEGINIF (SUBSTRING(@TableName, 1, 3) = 'PX_')
BEGIN
SELECT @TableName = 'dba.' + @TableName
END
--Dump the results of the sp_spaceused query to the temp table
INSERT #TempTable EXEC sp_spaceused @TableName
FETCH NEXT FROM tableCursor
INTO @TableName
END
CLOSE
tableCursor
DEALLOCATE
tableCursor
--Select all records so we can use the reults
SELECT tableName TableName,
numberofRows NumberofRows,
CAST(CASE WHEN CHARINDEX(' KB', reservedSize) > 0 THEN SUBSTRING(reservedSize, 1, CHARINDEX(' KB', reservedSize)) ELSE reservedSize END AS INT) ReservedSize,
CAST(CASE WHEN CHARINDEX(' KB', dataSize) > 0 THEN SUBSTRING(dataSize, 1, CHARINDEX(' KB', dataSize)) ELSE dataSize END AS INT) DataSize,
CAST(CASE WHEN CHARINDEX(' KB', indexSize) > 0 THEN SUBSTRING(indexSize, 1, CHARINDEX(' KB', indexSize)) ELSE indexSize END AS INT) IndexSize,
CAST(CASE WHEN CHARINDEX(' KB', unusedSize) > 0 THEN SUBSTRING(unusedSize, 1, CHARINDEX(' KB', unusedSize)) ELSE unusedSize END AS INT) UnusedSize
FROM #TempTable
ORDER BY 4 desc
DROP TABLE #TempTable
GO
No comments:
Post a Comment