SQLSERVER中统计所有表的记录数
利用系统索引表sysindexes中索引ID indid<1的行中的rows列存有该表的行数这一特点. 方法是利用隐藏未公开的系统存储过程sp_MSforeachtable
CREATE TABLE #temp (TableName VARCHAR (255), RowCnt INT) EXEC sp_MSforeachtable 'INSERT INTO #temp SELECT ''?'', COUNT(*) FROM ?' SELECT TableName, RowCnt FROM #temp ORDER BY RowCnt DESC DROP TABLE #temp