How much space do my SQL Server tables use?

The built-in stored procedure sp_spaceused generates a resultset containing the size of the database, or, when passed a table name, the size of a database table.

Using a cursor to iterate through all user tables, one can easily get a list of all tables and their sizes:

create table #t
(
  name nvarchar(128),
  rows varchar(50),
  reserved varchar(50),
  data varchar(50),
  index_size varchar(50),
  unused varchar(50)
)

declare @id nvarchar(128)
declare c cursor for
select name from sysobjects where xtype='U'

open c
fetch c into @id

while @@fetch_status = 0 begin

  insert into #t
  exec sp_spaceused @id

  fetch c into @id
end

close c
deallocate c

select * from #t
order by convert(int, substring(data, 1, len(data)-3)) desc

drop table #t

4 Responses to How much space do my SQL Server tables use?

  1. daniel says:

    Great !
    I used it (script) and solve my dudes….

  2. Ahmad says:

    Hi,
    Thanks for your script,
    What is the “unused” space and how can I reduce it?
    Thanks again

  3. Amir Ayub says:

    Thanks ……………… nice script

  4. Umasankar says:

    it is quite helpful. thanks

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.