Script to List Row Counts of All Tables
Something I had a use for the other day that I thought I would share. The following script can be run as-is against any SQL Server database, and will list one row per table and give you the row (record) count for each table. The results are sorted and returned as a single result set instead of multiple result sets as would happen if you used sp_MSforeachtable. The table count is not based on statistics, but rather based on actual counts. It uses a dirty read (read uncommitted) so as to not lock the table. I found this useful in a replication topology where I needed to generate a report comparing the counts between subscribers and publisher (you can use replication validation, but we needed actual counts more for a "sanity check").
Comments
- Anonymous
October 01, 2008
PingBack from http://blog.a-foton.ru/index.php/2008/10/02/script-to-list-row-counts-of-all-tables/ - Anonymous
October 01, 2008
Ouch, I hope those are not big tables - or else that will be a very painful script. You can also get the numbers from the clustered index stats as well. It might not be 'exact', but it is very close. - Anonymous
November 12, 2008
The comment has been removed