Search code examples
sqlsql-servershasha512

Get the SHA-512 of any SQL query


A common practice to compare two text file is to use the SHA-512 [or any other realted SHA algo]. If two SHA results are not the same then the files are not exactely the same.

I would like to do the same with two SQL queries. I just want to know if the queries gives 100% identical result or not with a SHA-512 [or sha-256 would be OK as well]?

Is that possible to perform that? I am using SQL Server...


Solution

  • Just to help...

    It's understood that both queries return the same columns in the same order.

    You must do:

    SELECT COUNT(*) FROM (
      ([YOUR_QUERY_A]
       EXCEPT
       [YOUR_QUERY_B]) -- A_B
      UNION ALL
      ([YOUR_QUERY_B]
       EXCEPT
       [YOUR_QUERY_A]) -- B_A
      ) EX
    

    If returns 0, both queries return the same

    For test purposes:

    SELECT COUNT(*) FROM (
      (select 1 a
       EXCEPT
       select 1)
      UNION ALL
      (select 1
       EXCEPT
       select 1)
      ) EX
    

    Change some inner query and look what changes