Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Illegal use of LONG datatype Oracle

this is my firt question here

I have a problem trying to convert a table column (long raw) to a base 64 string, this column contains some of the employees pictures.

This is the query, the field that i'm trying to convert is f.fot_empl:

SELECT e.NOM_EMPL First_name,
       APE_EMPL Last_name,
       e.NOM_EMPL || ' ' || e.APE_EMPL Full_name,
       car.NOM_CARG position,
       COS.NOM_CCOS Area,
       f.fot_empl Picture, 
       E.FEC_NACI Birth_date
  FROM EMPLE e
       INNER JOIN CONTR c
          ON E.COD_EMPL = C.COD_EMPL
       INNER JOIN cargo car
          ON C.COD_CARG = CAR.COD_CARG
       INNER JOIN CCOST cos
        on COS.COD_CCOS = C.COD_CCOS
       LEFT JOIN FOEMP f -- employee picture
          ON e.cod_empl = F.COD_EMPL
 WHERE C.IND_ACTI = 'A';

What i have tried:

The accepted answer of this post with no results, i keep getting "Illegal use of LONG datatype" error. Workaround for ORA-00997: illegal use of LONG datatype

I try to implement the following function with no results:

CREATE OR REPLACE FUNCTION to_base64 (
       vcodem     IN FOEMP.COD_EMPR%TYPE,
       vcodempl   IN FOEMP.COD_EMPL%TYPE)
       RETURN VARCHAR2
    IS
       V_VAR      FOEMP.FOT_EMPL%TYPE;
       V_result   VARCHAR2 (4000);
    BEGIN
       DBMS_OUTPUT.put_line ('Start');
       SELECT UTL_RAW.cast_to_varchar2 (
                 UTL_ENCODE.base64_encode (
                    UTL_RAW.cast_to_raw (DBMS_LOB.SUBSTR (f.FOT_EMPL, 4000))))
         INTO V_result
         FROM FOEMP f
        WHERE COD_EMPL = vcodempl AND COD_EMPR = vcodem;
       DBMS_OUTPUT.put_line ('End');
       DBMS_OUTPUT.put_line ('Result: ' || V_result);
    END to_base64;
    /

The function is invalid due to ORA-00997 in:

   SELECT UTL_RAW.cast_to_varchar2 (
             UTL_ENCODE.base64_encode (
                UTL_RAW.cast_to_raw (DBMS_LOB.SUBSTR (f.FOT_EMPL, 4000))))
     INTO V_result
     FROM FOEMP f
    WHERE COD_EMPL = vcodempl AND COD_EMPR = vcodem;

Many thanks in advance.

like image 822
diegofer11 Avatar asked Dec 04 '25 10:12

diegofer11


1 Answers

You can convert your LONG RAW value into a BLOB in a PL/SQL block, and then base64-encode that:

CREATE OR REPLACE FUNCTION to_base64 (
   vcodem     IN FOEMP.COD_EMPR%TYPE,
   vcodempl   IN FOEMP.COD_EMPL%TYPE)
   RETURN VARCHAR2
IS
  l_blob BLOB;
  l_long_raw LONG RAW;
BEGIN
  SELECT fot_empl INTO l_long_raw
    FROM foemp
   WHERE COD_EMPL = vcodempl AND COD_EMPR = vcodem;
  l_blob := TO_BLOB(l_long_raw);
  RETURN UTL_RAW.cast_to_varchar2 (UTL_ENCODE.base64_encode (l_blob));
END;
/

Of course, strongly recommends that you convert LONG RAW columns to binary LOB (BLOB) columns; still storing data as LONG or LONG raw seems rather antiquated now.

like image 72
Alex Poole Avatar answered Dec 07 '25 15:12

Alex Poole



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!