Wednesday, January 18, 2017

DB row count script

Please find the below script for the db row count.
Example: when we perform the db migration, at first we can keep the source server db to read only mode (consistency) and run this script.


--DB ROW COUNT:
============


BEGIN 
   SET NOCOUNT ON 

   CREATE TABLE #TableCounts
   ( 
      TableName VARCHAR(500), 
      CountOf INT 
   ) 

   INSERT #TableCounts
   EXEC sp_msForEachTable 
        'SELECT PARSENAME(''?'', 1), 
         COUNT(*) FROM ? WITH (NOLOCK)' 
/*
   SELECT TableName , CountOf 
   FROM #TableCounts
   ORDER BY TableName 
*/
   SELECT DB_NAME() DB, SUM(CountOf) AS total_row_count 
   FROM #TableCounts

   DROP TABLE #TableCounts
END

=======================================================
NOTE: AFTER THE RESTORATION ON THE DESTINATION SERVER, AGAIN WE CAN RUN THIS SCRIPT AND CHECK THE ROW COUNTS.(TESTING)



No comments:

Post a Comment