Compute blob hash in trigger on blob's table Compute blob hash in trigger on blob's table oracle oracle

Compute blob hash in trigger on blob's table


I executed the following code in Oracle PL/SQL:

var idn varchar2(18 byte);exec :idn := '0';insert into t_img (idn, img) values (:idn, utl_raw.cast_to_raw('0123456789') );commit;select idn, dbms_lob.getlength(img) as length, imghash from t_img where idn=:idn;update t_img set img=utl_raw.cast_to_raw('012345678901') where idn=:idn;commit;select idn, dbms_lob.getlength(img) as length, imghash from t_img where idn=:idn;

and it works

SQL*Plus: Release 11.2.0.1.0 Production on Sat Aug 31 11:20:04 2013

Copyright (c) 1982, 2010, Oracle. All rights reserved.

Connected to: Oracle Database 11g Release 11.2.0.1.0 - 64bit Production

PL/SQL procedure successfully completed.

1 row created. Commit complete.

IDN LENGTH IMGHASH


0 10 84D89877F0D4041EFB6BF91A16F0248F2FD573E6AF05C19F96BEDB9F882F7882

1 row updated. Commit complete.

IDN LENGTH IMGHASH


0 12 5CE9AB10EA74270D12620DFACD74D262C6411E20761E459BB1B265DE883422AC

I've found out that the error I posted happens only upon committing a manually edited IMG blob field in Toad (11.6.0.43 free version) but not when I exercise the table (inserts, updates) via PL/SQL.

AdiM


I had to do this trick:

create or replace TRIGGER DESPACHO_ACTA_ARCHIVO_IU BEFORE INSERT OR UPDATE  ON CAT_DESPACHO_ACTA_ARCHIVO FOR EACH ROWDECLARE TEMPFLD BLOB;blobid number;BEGINSELECT BLOBTEMP_SEQ.NEXTVAL INTO blobid FROM SYS.DUAL;INSERT INTO blobtemp  VALUES (:new.bacta,blobid);SELECT blobfield INTO :new.bacta FROM blobtemp where id =blobid ;delete from blobtemp where id =blobid ;:NEW.SHEX_SHA1 := sha2_blob(:new.bacta,1); END;

Create a temporarly table to force blob to persist and obtain the correct hash value