Skip to content
Advertisement

Is there an Azure SQL Database equivalent to MySQL’s “CHECKSUM TABLE”, and should it be used to check if two tables are identical?

I was looking for a way to check if two tables in the same database are identical (both contents and schema), and came over this question.

The top answer for the question uses (from my understanding) a MySQL specific. When trying to run the code in an Azure SQL Database I get this error: Incorrect syntax near the keyword 'TABLE'..

Instead of using CHECKSUM TABLE to see if the two tables are identical we ended up using the two following queries:

select * from util.table1
except
select * from dbo.table2

select * from dbo.table2
except
select * from util.table1

So I essentially have three questions regarding this:

  1. Does Azure SQL Database have an equivalent to this query?
  2. Should we even use that query for checking if two tables are identical?
  3. Is the option we went for (using except) a viable option? if not, why not, and what can we do better?

Advertisement

Answer

The CHECKSUM function in Azure SQL Database returns the checksum value computed over a table row, or over an expression list. Checksum returns the hash value over its argument list.

Example:

In the below example, the checksum value returns the computed value of the table row.

enter image description here

To compare the data from two tables, the best way is to use the EXCEPT.

EXCEPT returns any distinct values from the query left of the EXCEPT operator. Those values return as long the right query doesn’t return those values as well.

Tables data:

enter image description here

With Except:

enter image description here

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement