Statistics on the size of SQL Server user data tables (including the total number of records and space usage) in SQL Server, a simple combination of sp_spaceused and sp_msforeachtable stored procedures, you can easily calculate the size of the user data table, this includes the total number of records and space usage. It is very practical and has been tested in both sqlserver2k and sqlserver2005.
/**/ /*
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.