Technical Article

Get record count for every table in a database

,

I wrote this code to make it easier to compare record counts between a live database and a restored copy to test my backups, I thought people might find it useful. You need to run it against your live side when you do the backup that you're going to restore and compare against.

Copy the output into another Query Analyzer window and run it: instant record counts.

Suck the results into Excel, run the script again against the restored DB, and you should be able to see any problems fairly quickly. I would expect perhaps a minor variation in counts, this should at least show you any radical changes.

I've only tested this under SQL Server 2000 SP3a.

print '--delete the final UNION before the ORDER BY'
print '--  before running this script.'
print '--'
go

exec sp_msforeachtable @command1="print 'SELECT ''?'' AS TableName, COUNT(*) as TblRowCount FROM ?' print 'UNION'"
go
print 'ORDER BY TableName'
go

Rate

5 (1)

You rated this post out of 5. Change rating

Share

Share

Rate

5 (1)

You rated this post out of 5. Change rating