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:
- Does Azure SQL Database have an equivalent to this query?
- Should we even use that query for checking if two tables are identical?
- 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.
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:
With Except: