|
|
Cry How to...List the size of each table in the databaseIt is sometimes useful to obtain the current storage requirements of each table in a database, i.e. the number of KB of storage currently occupied by each table. There are a number of ways to get the size of tables in a SQL Server database: : Using Enterprise ManagerSQL Server Enterprise Manager can show the space used by each table in a database:
Using sp_spaceused (for single table)If you only require the space used for a single table then use
the stored procedure sp_spaceused 'accounts' this returns the name, rows, reserved space, data space, index-size and unused space allocated to the table. If you require this for a number of tables then either call Using a scriptThe following script calls sp_spaceused for every user table in the current database. It returns these results in a single table. I have found it useful to include space allocation for the entire table as well, but if this is not required then delete the last line of the script. set nocount on create table #spaceused ( name nvarchar(120), rows char(11), reserved varchar(18), data varchar(18), index_size varchar(18), unused varchar(18) ) declare Tables cursor for select name from sysobjects where type='U' order by name asc OPEN Tables DECLARE @table varchar(128) FETCH NEXT FROM Tables INTO @table WHILE @@FETCH_STATUS = 0 BEGIN insert into #spaceused exec sp_spaceused @table FETCH NEXT FROM Tables INTO @table END CLOSE Tables DEALLOCATE Tables select * from #spaceused drop table #spaceused exec sp_spaceused These notes have been tested against SQL Server 7 and SQL Server 2000. |