This script is used to query the number of records of all tables in the current database.

Source: Internet
Author: User

-- ===================================================== ==========================================
-- 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

 

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.