Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Oracle STANDARD_HASH not available in PLSQL?

Tags:

hash

oracle

plsql

I'm trying to use STANDARD_HASH Oracle (12c) function in PL/SQL but seems not available:

SQL> exec   dbms_output.put_line(STANDARD_HASH('test'));
BEGIN dbms_output.put_line(STANDARD_HASH('test')); END;

                           *
ERROR at line 1:
ORA-06550: line 1, column 28:
PLS-00201: identifier 'STANDARD_HASH' must be declared
ORA-06550: line 1, column 7:
PL/SQL: Statement ignored

From sql is working just fine:

SQL> select STANDARD_HASH('test') from dual;

STANDARD_HASH('TEST')
----------------------------------------
A94A8FE5CCB19BA61C4C0873D391E987982FBBD3

Why? What is the best way to implement the STANDARD_HASH in PLSQL?

Regards

like image 554
StefanG Avatar asked Feb 23 '16 10:02

StefanG


Video Answer


2 Answers

Seems like it isn't yet a part of PL/SQL in 12c.

As a workaround, use SELECT INTO in PL/SQL:

SQL> set serveroutput on
SQL> DECLARE
  2    str VARCHAR2(40);
  3  BEGIN
  4    SELECT STANDARD_HASH('test') INTO str FROM dual;
  5    dbms_output.put_line(str);
  6  END;
  7  /
A94A8FE5CCB19BA61C4C0873D391E987982FBBD3

PL/SQL procedure successfully completed.

SQL>

I would suggest to create a function, and use it whenever you need it in PL/SQL.

For example,

SQL> CREATE OR REPLACE FUNCTION STANDARD_HASH_OUTPUT(str IN VARCHAR2)
  2    RETURN VARCHAR2
  3  AS
  4    op VARCHAR2(40);
  5  BEGIN
  6    SELECT STANDARD_HASH(str) INTO op FROM dual;
  7    RETURN op;
  8  END;
  9  /

Function created.

Call the function directly in PL/SQL block:

SQL> BEGIN
  2     dbms_output.put_line(STANDARD_HASH_OUTPUT('test'));
  3  END;
  4  /
A94A8FE5CCB19BA61C4C0873D391E987982FBBD3

PL/SQL procedure successfully completed.

SQL>
like image 175
Lalit Kumar B Avatar answered Sep 25 '22 08:09

Lalit Kumar B


For text, STANDARD_HASH is the same as DBMS_CRYPTO.HASH with SHA1:

begin
  dbms_output.put_line(dbms_crypto.hash(cast('test' as clob), dbms_crypto.hash_sh1));
end;
/

Output:

A94A8FE5CCB19BA61C4C0873D391E987982FBBD3

For other data types, it's not documented how they are passed to the hash function.

like image 32
Codo Avatar answered Sep 21 '22 08:09

Codo