PL/SQL Raw datatype variable comparison

Andrey Khataev picture Andrey Khataev · Feb 10, 2012 · Viewed 9.1k times · Source

Is it possible to compare variables of raw datatypes? I'm working with XMLDOM.DomNodes, which is records with one field by itself:

TYPE DOMNode IS RECORD (id RAW(12));

So I have two nodes, then could I compare them by their id fields? I tried several samples and at first glance it seems to work:

  FUNCTION findParentNode(p_node IN xmldom.domnode) RETURN PLS_INTEGER
  AS
      nRetVal               PLS_INTEGER;
  BEGIN
     FOR i IN ParentNodes.First .. ParentNodes.Last
     LOOP 
         IF ParentNodes(i).id = p_node.id THEN               
            nRetVal := i;
            EXIT;
         END IF;         
     END LOOP;

     RETURN nRetVal;
  END;

but one thing in Oracle documentation worries me: Raw data is like VARCHAR2 data, except that PL/SQL does not interpret raw data What does it mean? If pl/sql doesn't interpret raw, then could it compare?

Answer

Vincent Malgrat picture Vincent Malgrat · Feb 10, 2012

You can use the = operator if you want to see if two RAW variables have the same values.

SQL> DECLARE
  2     a RAW(255) := utl_raw.cast_to_raw('abcdef');
  3     b RAW(50) := utl_raw.cast_to_raw('abcdef');
  4  BEGIN
  5     IF a = b THEN
  6        dbms_output.put_line('a = b');
  7     ELSE
  8        dbms_output.put_line('a != b');
  9     END IF;
 10  END;
 11  /
a = b

When the documentation states that RAW is like VARCHAR2, but not interpreted, it means that you can affect, store and even compare RAW variables just like you would VARCHAR2, but the binary value is never matched against a character set.

VARCHAR2 variables, on the other hand, can be converted because of a mismatch between the character sets of the database and the client.

RAW is a string of bytes instead of a string of characters.