SQL Count Records in all Tables

Created by Chris Sandu, Modified on Wed, 19 Feb, 2025 at 4:12 PM by Chris Sandu

Count Records in all Tables


Sign in to Trapper (Start, Run, mstsc, Trapper, password)


Open Microsoft SQL Server Management Studio


Highlight the database you want to count records in

New Query, you should see the database name near top (under the New Query button)

 

Paste the following in New Query in SQL:


 CREATE TABLE #counts

(

    table_name varchar(255),

    row_count int

)


EXEC sp_MSForEachTable @command1='INSERT #counts (table_name, row_count) SELECT ''?'', COUNT(*) FROM ?'

SELECT table_name, row_count FROM #counts ORDER BY table_name, row_count DESC



Execute

See Results window


Any table names that start with "a" are the add tables.

Any table names that start with "d" are the delete tables.

The number following the "a" or "d" is the table registration_id

If the count is not zero in all the add and delete tables the database didn't compress correctly.


If you open Tables in tree on left and look near the very bottom at ownername_table registry it will give you the table name that corresponds to the table registration_idnumber in the query results.


To see information in _table_registry, right click, select all row


When finished close the query tabs and if this was just a one time query, don’t save the query.


Was this article helpful?

That’s Great!

Thank you for your feedback

Sorry! We couldn't be helpful

Thank you for your feedback

Let us know how can we improve this article!

Select at least one of the reasons

Feedback sent

We appreciate your effort and will try to fix the article