In sqlserver, a simple combination of sp_spaceused and sp_msforeachtable stored procedures can easily calculate the size of the user data table, including the total number of records and space usage, which is very practical, both sqlserver2k and sqlserver2005 passed the test.
/**/ /*
1. Exec sp_spaceused 'table name' (SQL statistics, which may be inaccurate after a large number of transaction operations)
2. Exec sp_spaceused 'table name', true (update the table space size, accurate empty table size, but it may take some time)
3. Exec sp_spaceused (database size query)
4. Exec sp_msforeachtable "Exec sp_spaceused '? '"(All user tablespace tables are small, SQL statistics, and may be inaccurate after a large number of transaction operations)
5. Exec sp_msforeachtable "Exec sp_spaceused '? ', True "(all user tablespace tables are small, and large databases are used with caution)
*/
Create Table # T (name Varchar ( 255 ), Rows Bigint , Reserved Varchar ( 20 ), Data Varchar ( 20 ), Index_size Varchar ( 20 ), Unused Varchar ( 20 ))
Exec Sp_msforeachtable" Insert Into # T Exec Sp_spaceused ' ? ' "
Select * From # T
Drop Table # T
In addition, sp_msforeachdb can traverse all databases. For more information, see SQL help.