in a DB2 trigger, I need to compare the value of a CLOB field. Something like:
IF OLD_ROW.CLOB_FIELD != UPDATED_ROW.CLOB_FIELD
but "!=" does not work for comparing CLOBs.
What is the way to compare it?
Edited to add:
My trigger needs to do some action if the Clob field was changed during an update. This is the reason I need to compare the 2 CLOBs in the trigger code. I'm looking for some detailed information on how this can be done
compare in SQL to compare if two CLOB columns are matching in two separate tables: For using dbms_lob. compare in SQL, you can compare two LOB columns within a single table, joining the table against itself, like this example comparing the CLOB column col1 with CLOB column col2. a. tab_key = b.
CLOBs require that you use the DBMS_LOB package to perform substr/instr type searches. In order to use do any kind of searching within the column, you must first get the locator for the CLOB column, the use the DBMS_LOB. SUBSTR or DBMS_LOB. INSTR function to search and/or pull part of the text from the string.
Try this: declare Data1 Clob; begin Data1 := 'ab c d'; -- replace all the whitespace in Data1 with null if dbms_lob. getlength(regexp_replace(Data1,'[[:space:]]'))=0 then dbms_output. put_line('First try: Data1 is null'); else dbms_output.
In Oracle 10g you can use DBMS_LOB.compare() API.
Example:
select * from table t where dbms_lob.compare(t.clob1, t.clob2) != 0
Full API:
DBMS_LOB.COMPARE (
lob_1 IN BLOB,
lob_2 IN BLOB,
amount IN INTEGER := 4294967295,
offset_1 IN INTEGER := 1,
offset_2 IN INTEGER := 1)
RETURN INTEGER;
DBMS_LOB.COMPARE (
lob_1 IN CLOB CHARACTER SET ANY_CS,
lob_2 IN CLOB CHARACTER SET lob_1%CHARSET,
amount IN INTEGER := 4294967295,
offset_1 IN INTEGER := 1,
offset_2 IN INTEGER := 1)
RETURN INTEGER;
DBMS_LOB.COMPARE (
lob_1 IN BFILE,
lob_2 IN BFILE,
amount IN INTEGER,
offset_1 IN INTEGER := 1,
offset_2 IN INTEGER := 1)
RETURN INTEGER;
Calculate the md5 (or other) hash of the clobs and then compare these. Initial calculation will be slow but comparison is fast and easy. This could be a good method if the bulk of your data doesn't change very often.
One way to calculate md5 is through a java statement in your trigger. Save these in the same table (if possible) or build a simple auxiliary table.
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