In SQL Server, how does one count the space occupied by all user tables in the database?

Source: Internet
Author: User

Script:

Create Table # data (name varchar (100), row varchar (100), reserved varchar (100), data varchar (100 ),

Index_size varchar (100), unused varchar (100); Create Table # datanew (name varchar (100), row int, reserved int, data int, index_size int, unused INT)

Declare @ name varchar (100 );

Declare cur cursor

Select name from sysobjects where xtype = 'U' order by name

Open cur

Fetch next from cur into @ name

While @ fetch_status = 0

Begin

Insert into # data exec sp_spaceused @ name

Fetch next from cur into @ name

End

Close cur

Deallocate cur

Insert into # datanew select name, convert (INT, row) as row, convert (INT, replace (reserved, 'kb', '') as reserved, convert (INT, replace (data, 'kb', '') as data,

Convert (INT, replace (index_size, 'kb', '') as index_size, convert (INT, replace (unused, 'kb ','')) as unused from # data;

Select * from # datanew order by data DESC;

Drop table # data;

Drop table # datanew;

Result column description:

Name: Table Name

Row: Total number of rows in the table

Reserved: total reserved table space

Data: total space used by the data in the table

Index_size:TableTotal space used by the index in

Unused: Total amount of space reserved for the table but not used

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.