Oracle STANDARD_HASH not available in PLSQL?

2020-08-17 05:19发布

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

2条回答
Viruses.
2楼-- · 2020-08-17 06:06

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.

查看更多
聊天终结者
3楼-- · 2020-08-17 06:10

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>
查看更多
登录 后发表回答