Search code examples
oracle-databaseplsqlplsqldeveloperplsql-package

how to use dbms_lob.substr in sql*plus


Given a table:

desc SOL_3_ARTIFACTS
Name Null Type          
---- ---- ------------- 
DATA      BLOB          
ID        VARCHAR2(100) 

If I need to read content of a BLOB column as VARCHR2 with SQL*plus, I'd need a statement like this:

select UTL_RAW.CAST_TO_VARCHAR2(dbms_lob.substr(lob_loc,2000,1)) 
from SOL_3_ARTIFACTS where ID = 'RETAILPOOLS'

would anyone tell me how to get the lob_loc (that is, the locator of the DATA column) for a particular row (ID = 'RETAILPOOLS')?

per Belayer's recommendation, I run the follwing but got errors:

DECLARE
     fil BFILE; 
     pos INTEGER;
     amt BINARY_INTEGER; 
     buf RAW(40); 
BEGIN 
     SELECT SOL_3_ARTIFACTS.DATA INTO fil from SOL_3_ARTIFACTS where ID = 'RETAILPOOLS';
     dbms_lob.open(fil, dbms_lob.lob_readonly);   
     amt := 40; pos := 1 + dbms_lob.getlength(fil); buf := ''; 
     dbms_lob.read(fil, amt, pos, buf); 
     dbms_output.put_line('Read F1 past EOF: '|| 
          utl_raw.cast_to_varchar2(buf)); 
     dbms_lob.close(fil); 
     exception 
     WHEN no_data_found 
     THEN 
       BEGIN 
         dbms_output.put_line('End of File reached. Closing file'); 
         dbms_lob.fileclose(fil); 
         -- or dbms_lob.filecloseall if appropriate 
       END; 
END; 

Error report -
ORA-06550: line 7, column 13:
PL/SQL: ORA-00932: inconsistent datatypes: expected FILE got BLOB
ORA-06550: line 7, column 6:
PL/SQL: SQL Statement ignored
ORA-06550: line 8, column 25:
PLS-00201: identifier 'DBMS_LOB' must be declared
ORA-06550: line 8, column 6:
PL/SQL: Statement ignored
ORA-06550: line 9, column 28:
PLS-00201: identifier 'DBMS_LOB' must be declared
ORA-06550: line 9, column 17:
PL/SQL: Statement ignored
ORA-06550: line 10, column 6:
PLS-00201: identifier 'DBMS_LOB' must be declared
ORA-06550: line 10, column 6:
PL/SQL: Statement ignored
ORA-06550: line 13, column 6:
PLS-00201: identifier 'DBMS_LOB' must be declared
ORA-06550: line 13, column 6:
PL/SQL: Statement ignored
ORA-06550: line 19, column 10:
PLS-00201: identifier 'DBMS_LOB' must be declared
ORA-06550: line 19, column 10:
PL/SQL: Statement ignored
06550. 00000 -  "line %s, column %s:\n%s"
*Cause:    Usually a PL/SQL compilation error.
*Action:

Solution

  • Try starting here

    https://docs.oracle.com/cd/A91202_01/901_doc/appdev.901/a88879/adl02bs4.htm

    Especially this section pasted below which points out that when you select the blob it returns the lob locator.

    Accessing a LOB Through a Locator

    SELECTing a LOB Performing a SELECT on a LOB returns the locator instead of the LOB value. In the following PL/SQL fragment you select the LOB locator for story and place it in the PL/SQL locator variable Image1 defined in the program block. When you use PL/SQL DBMS_LOB functions to manipulate the LOB value, you refer to the LOB using the locator.

    DECLARE
        Image1       BLOB;
        ImageNum     INTEGER := 101;
    BEGIN
        SELECT story INTO Image1 FROM Multimedia_tab
            WHERE clip_id = ImageNum;
        DBMS_OUTPUT.PUT_LINE('Size of the Image is: ' ||
            DBMS_LOB.GETLENGTH(Image1));
        /* more LOB routines */
    END;