Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get hash values from SQL Server and Oracle and compare them?

Is it possible to generate hash code from both database server and compare them? How to write the following pseudo SQL in SQL Server? Especially the two getHash functions which accept mutliple numeric/float columns in SQL server and oracle.

select s.PK
from sqltable s
    join openquery(oracleLinkedServer, 
      'select PK, getHash(Column1, floatColumn2, ..., floatColumnN) oracleHash 
       from oracleTable') o on o.PK = s.PK
where
    getHash(Column1, floatColumn2, ..., floatColumnN) <> oracleHash
like image 762
ca9163d9 Avatar asked Jan 10 '12 21:01

ca9163d9


People also ask

What is SQL hash value Oracle?

The SQL ID is a hash of the text of the SQL statement. Similarly, the hash_value is a hash of the statement itself. You expect these to be the same every time you run a statement. The plan_hash_value is a hash of the execution plan. This will change if you get a different plan.

What is standard hash in Oracle?

STANDARD_HASH computes a hash value for a given expression using one of several hash algorithms that are defined and standardized by the National Institute of Standards and Technology.

What is the use of HashBytes in SQL Server?

SQL Server has a built-in function called HashBytes to support data hashing. A good hashing algorithm has these properties: It is especially sensitive to small changes in the input. Minor changes to the document will generate a very different hash result.

What is hashing algorithm in Oracle?

ORA_HASH is a function that computes a hash value for a given expression. This function is useful for operations such as analyzing a subset of data and generating a random sample. The expr argument determines the data for which you want Oracle Database to compute a hash value.


2 Answers

In SQL Server:

select upper(substring(sys.fn_sqlvarbasetostr(hashbytes('MD5','A')),3,32));

result:

7FC56270E7A70FA81A5935B72EACBE29

In Oracle :

select rawtohex(
    DBMS_CRYPTO.Hash (
        UTL_I18N.STRING_TO_RAW ('A', 'AL32UTF8'),
        2)
    ) from dual;

result:

7FC56270E7A70FA81A5935B72EACBE29

Make sure your strings are exactly the same (case sensitive). Here I used 'A' as a simple example, but it could be any string really.

If you avoid data type differences by converting to a big string, you should be able to produce the same md5 hash on different platforms. Note that SQL Server prepended a '0x' to the hash to denote hex representation, which I stripped with the substring.

like image 119
tbone Avatar answered Oct 24 '22 11:10

tbone


In SQL Server you have hashbytes(); in Oracle you have DBMS_CRYPTO.Hash(). You should be able to use them to calc an MD5 hash on both sides, though I am not positive the hashes will match... its worth a shot.

There are other ways to compare tables but to answer your question these are the two native functions on either platform.

like image 22
nathan_jr Avatar answered Oct 24 '22 11:10

nathan_jr