Monday, August 27, 2007

SQL Server: Get row count for each table

This T-SQL (for SQL Server 2000 and 2005) script I just found will print out the number of rows for each table in the current database:

create table #rowcount (tablename varchar(128), rowcnt int)
exec sp_MSforeachtable 'insert into #rowcount select ''?'', count(*) from ?'
select * from #rowcount order by rowcnt desc
drop table #rowcount

The result will look like this: