MSSQL Script for getting row COUNT of each table in the database

Description:
This script gets the row counts of each table in the database without doing a COUNT. This script will help us to compare row count in two different databases so we can make sure that DB move or DB conversion is done without any data loss.

Script:

SELECT t.name [TableName], SUM(st.row_count)[RowCount]
    FROM sys.tables t
        JOIN sys.schemas s ON t.schema_id = s.schema_id
        JOIN sys.indexes i ON t.object_id = i.object_id
        JOIN sys.dm_db_partition_stats st ON t.object_id = st.object_id AND i.index_id = st.index_id
    WHERE i.index_id < 2
        -- Heaps and Clustered IndexesGROUPBY s.name, t.name
        GROUP BY t.name
        ORDER BY  t.name;

For an example, consider following screenshot:

MSSQL Script for getting row COUNT of each table in the database

 

 

 

 

 

 

 

 

 

 

 

 

 


<<Click here to see all posts>>

 

Leave a Reply

Your email address will not be published. Required fields are marked *

Prove You Are Human Time limit is exhausted. Please reload CAPTCHA.