Check Data Consistency SQL Level (Database level and Table Level)

Ardan Zaki 236 Reputation points
2022-11-23T07:38:01.547+00:00

Hi everyone, I currently partition my tables using copy of the data to empty table method.

The point is, I copied the whole data from one table to a new table with the exact same column, what should I do to make sure that all data between two tables is exactly the same?

*The tables are huge some tables reach 1TB

SQL Server Migration Assistant
SQL Server Migration Assistant
A Microsoft tool designed to automate database migration to SQL Server from Access, DB2, MySQL, Oracle, and SAP ASE.
568 questions
SQL Server Other
{count} votes

1 answer

Sort by: Most helpful
  1. Muhammad Ahsan Khan 250 Reputation points
    2023-03-27T10:57:16.1366667+00:00

    To check data consistency between two tables in SQL Server, you can perform a row count comparison and a checksum comparison. Here are the steps:

    1. Perform a row count comparison: Count the number of rows in each table and compare them. If the counts are the same, it's a good indication that the data is consistent. To count the rows, you can use the following query: SELECT COUNT(*) FROM table_name;
    2. Perform a checksum comparison: Calculate a checksum value for each table and compare them. A checksum is a numeric value that represents the contents of a table. If the checksum values are the same, it's a strong indication that the data is consistent. To calculate the checksum, you can use the following query: SELECT CHECKSUM_AGG(BINARY_CHECKSUM()) FROM table_name; Note that BINARY_CHECKSUM() calculates a checksum for all columns in the table, while CHECKSUM_AGG aggregates the checksum values for all rows.
    3. Compare the results: Compare the row counts and checksum values for both tables. If they match, the data is consistent. If they don't match, there may be some data discrepancies between the two tables.

    To perform these checks at the database level, you can use a script to loop through all the tables in the database and perform the row count and checksum comparisons. Here is an example script:

    DECLARE @table_name nvarchar(128) DECLARE @sql nvarchar(max)

    DECLARE table_cursor CURSOR FOR SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE='BASE TABLE'

    OPEN table_cursor

    FETCH NEXT FROM table_cursor INTO @table_name

    WHILE @@FETCH_STATUS = 0 BEGIN SET @sql = N'SELECT ''' + @table_name + ''', COUNT(), CHECKSUM_AGG(BINARY_CHECKSUM()) FROM ' + @table_name EXEC sp_executesql @sql

    FETCH NEXT FROM table_cursor INTO @table_name END

    CLOSE table_cursor DEALLOCATE table_cursor

    This script will loop through all the base tables in the database, calculate the row counts and checksum values, and print the results to the console. You can then compare the results to identify any data inconsistencies.

    Hope this helps!

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.