Get the size of your tables

Get the size of your tables

Recently my boss asked me, why the databases (Microsoft SQL Server 2005) of our customers are so big. With the following SQL-Statements I could give more or less an answer to my boss.

CREATE TABLE PW_SPACE(
name varchar(255),
rows int,
reserved varchar(255),
data varchar(255),
index_size varchar(255),
unused varchar(255)
)
GO

EXEC sp_MSforeachtable @command1="INSERT INTO PW_SPACE EXEC sp_spaceused '?'"
GO

select * from PW_SPACE order by rows desc
GO

select sum(rows) as Rows,
convert(varchar(255), sum(convert(int, substring(data,0,len(data)-2)))) + ' KB' as Data,
convert(varchar(255), sum(convert(int, substring(index_size,0,len(index_size)-2)))) + ' KB' as Indexsize
from PW_SPACE
GO

2 thoughts on “Get the size of your tables

  1. You know, the thing about SQL is, that there is virtually nothing that can replace it.

    Does anyone know if a substitute exists for sql? I mean besides MS SQL and Oracle and all that jazz. Thanks.

  2. This won’t do it. Probably most of your space is in the logs. You need to add that to the list of the space. But this is a good start.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.