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
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.
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With