-- ===================================================== ==========================================
-- Note: This script is used to query the number of records in all tables in the current database.
-- And save the result to the tableinfo table. It will not be deleted for further processing and analysis.
-- However, please delete the table.
-- ===================================================== ==========================================
If exists (select * From DBO. sysobjects where id = object_id (n' [DBO]. [tablespace] ') and objectproperty (ID, n'isusertable') = 1)
Drop table [DBO]. [tablespace]
Go
Create Table tablespace
(
Tablename varchar (20 ),
Rowscount char (11 ),
Reserved varchar (18 ),
Data varchar (18 ),
Index_size varchar (18 ),
Unused varchar (18)
)
Go
Declare @ SQL varchar (500)
Declare @ tablename varchar (20)
Declare cursor1 cursor
For
Select name from sysobjects where xtype = 'U'
Open cursor1
Fetch next from cursor1 into @ tablename
While @ fetch_status = 0
Begin
Set @ SQL = 'insert into tablespace'
Set @ SQL = @ SQL + 'exec sp_spaceused ''' + @ tablename + ''''
Exec (@ SQL)
Fetch next from cursor1 into @ tablename
End
Close cursor1
Deallocate cursor1
Go
-- Display Results
Select * From tablespace
-- Order by tablename
-- Order by tablename ASC -- by table name, used to count tables
-- Order by rowscount desc -- displays the number of rows in the table.
-- Order by reserved DESC, data desc -- by occupied space
-- Order by index_size DESC, reserved desc -- View by index space
Go
-- View the usage of the database, which can be executed at any time.
-- Exec sp_spaceused
-- Go