如何在 sql*plus 中使用 dbms_lob.substr

how to use dbms_lob.substr in sql*plus

给定一个 table:

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

如果我需要使用 SQL*plus 将 BLOB 列的内容读取为 VARCHR2,我需要这样的语句:

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

谁能告诉我如何获取特定行 (ID = 'RETAILPOOLS') 的 lob_loc(即 DATA 列的定位器)?

根据 Belayer 的建议,我 运行 以下但出现错误:

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:

尝试从这里开始

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

特别是下面粘贴的这一部分指出,当您 select blob 时,它 returns lob 定位器。

通过定位器访问 LOB

SELECT创建 LOB 对 LOB returns 执行 SELECT 定位符而不是 LOB 值。在下面的 PL/SQL 片段中,您 select 故事的 LOB 定位器并将其放置在程序块中定义的 PL/SQL 定位器变量 Image1 中。当您使用 PL/SQL DBMS_LOB 函数来操作 LOB 值时,您使用定位器引用 LOB。

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;