Paul Duffett – Me, myself and MS SQL Server

The ramblings of a mad man

Space / usage per table

leave a comment »

Hi all,

Just a very, very quick post because I’m still at work :D

This very short script will loop through all of your (user created) tables and return the number of rows and space used. Very useful indeed.

SET NOCOUNT OFF

DECLARE @tblName VARCHAR(255)
DECLARE tblCursor CURSOR FOR
SELECT name FROM sysobjects WHERE type = ‘U’ ORDER BY name

OPEN tblCursor

FETCH NEXT FROM tblCursor
INTO @tblName

WHILE @@FETCH_STATUS = 0
BEGIN
  
   EXEC sp_spaceused @tblName

   FETCH NEXT FROM tblCursor
   INTO @tblName
END

CLOSE tblCursor
DEALLOCATE tblCursor

Enjoy

PD

Written by paulduffett

October 28, 2008 at 4:44 pm

Posted in useful scripts

Leave a Reply