SQL Server View all table sizes, occupied space

Source: Internet
Author: User

--Main principle:
EXEC sp_spaceused ' table name '--get table occupy space
EXEC sp_spaceused '--all the space in the repository


CREATE TABLE #Data (name varchar), row varchar (+), reserved varchar (+), Data varchar (+), index_size varchar (100) , unused varchar (100))

DECLARE @name varchar (100)
DECLARE cur cursor FOR
Select name from sysobjects where xtype= ' u ' ORDER by name
Open cur
FETCH NEXT from cur to @name
While @ @fetch_status =0
Begin
INSERT INTO #data
EXEC sp_spaceused @name
Print @name

FETCH NEXT from cur to @name
End
Close cur
Deallocate cur

CREATE TABLE #DataNew (name varchar, row int,reserved int,data int,index_size int,unused int)

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

SQL Server View all table sizes, occupied space

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.