If not exists (select * From DBO. sysobjects where id = object_id (n' [DBO]. [tablespaceinfo] ') and objectproperty (ID, n' isusertable') = 1)
Create Table tablespaceinfo -- create a temporary table
(Nameinfo varchar (50), -- table name
Rowsinfo int, -- number of existing rows in the table
Reserved varchar (20), -- total table space
Datainfo varchar (20), -- table data space
Index_size varchar (20), -- the amount of space used by the table Index
Unused varchar (20) -- amount of unused space in the table
Delete from tablespaceinfo
Declare @ tablename varchar (255)
Declare @ brief SQL varchar (500)
Declare info_cursor cursor
Select name
From DBO. sysobjects where objectproperty (ID, N 'istable') = 1
And name not like n' # % 'order by name
Open info_cursor
Fetch next from info_cursor
Into @ tablename
While @ fetch_status = 0
Begin
If exists (select * From DBO. sysobjects where id = object_id (@ tablename) and objectproperty (ID, N 'isusertable') = 1)
Execute sp_executesql
N 'insert into tablespaceinfo exec sp_spaceused @ tbname ',
N' @ tbname varchar (255 )',
@ Tbname = @ tablename
Fetch next from info_cursor
Into @ tablename
End
Close info_cursor
Deallocate info_cursor
Go
-- Tablespace Information
Select *
From tablespaceinfo
Order by cast (left (ltrim (rtrim (Reserved), Len (ltrim (rtrim (Reserved)-2) As INT) DESC