Chapter 16. DBMS_LOB

Table of Contents

16.1. Overview
16.2. Exceptions
16.3. Procedures
16.3.1. APPEND
16.3.2. COPY
16.3.3. CONVERTTOBLOB
16.3.4. CONVERTTOCLOB
16.3.5. CREATETEMPORARY
16.3.6. ERASE
16.3.7. FILECLOSE
16.3.8. FILECLOSEALL
16.3.9. FILEGETNAME
16.3.10. FILEOPEN
16.3.11. FREETEMPORARY
16.3.12. LOADFROMFILE
16.3.13. LOADBLOBFROMFILE
16.3.14. OPEN
16.3.15. READ
16.3.16. TRIM
16.3.17. WRITE
16.3.18. WRITEAPPEND
16.4. Functions
16.4.1. CLOSE
16.4.2. COMPARE
16.4.3. FILEEXISTS
16.4.4. FILEISOPEN
16.4.5. GETLENGTH
16.4.6. INSTR
16.4.7. ISOPEN
16.4.8. ISTEMPORARY
16.4.9. SUBSTR

This chapter briefly introduces the DBMS_LOB package and describes how to use the procedures and functions included of the package.

16.1. Overview

DBMS_LOB is a package that provides various operations for BLOB, CLOB, or BFILE column data. Using the procedures and functions in this package, users can read and write all or part of large object data.

The following information needs to be understood when using DBMS_LOB:

  • Privileges of the caller

    • This package can be executed with the privileges of the caller. If invoked by other procedures or functions, it is executed using the privileges of the executor of the procedure or function.

  • Parameter length and offset units of procedures and functions

    • If the destination data type is BLOB, the units are in bytes, and if it is CLOB, the units are in characters. The return value of the function uses the same units.

  • Valid parameter value range of procedures and functions

    • The offset, length, and size of LOB data must always be greater than 1 and less than the LOBMAXSIZE constant value.

    • The LOBMAXSIZE constant has the value 18446744073709551615 (2^64 – 1).

    • If the offset, length, or size of LOB data is less than 1 or greater than LOBMAXSIZE, an INVALID_ARGVAL exception will occur.

  • CLOB column data

    • This type saves strings in UTF-16 Unicode character set, and each letter is represented with two bytes.

    • The size should be less than the value of the LOBMAXSIZE constant divided by 2.

    • If the size of a CLOB data is greater than this value, an INVALID_ARGVAL exception will occur.

  • APPEND, COPY, TRIM, WRITE

    • These are the parameters of a procedure or function that is used to update LOB data, and the sum of the size and offset values must not exceed the maximum value (the value of the LOBMAXSIZE constant). If it is exceeded, an exception will occur. To update LOB data, its row must be locked since the procedure or function does not automatically set locks.

  • READ, COMPARE, INSTR, SUBSTR

    • These read-only procedures and functions read only until the end of the LOB data.

  • COMPARE, INSTR, SUBSTR

    • These functions perform string-pattern related operations. Wild card characters used with the LIKE operator, such as the percent sign (%) or underscore (_), cannot be used in these parameters.

When using LOB data, the data must be opened using the OPEN procedure, and closed using the CLOSE procedure after use. When LOB data, which is opened using the OPEN procedure, is closed the updates are applied to the database.

When LOB data that was not opened with the OPEN procedure is updated, the update will be immediately applied to the database. In most cases, LOB data updates require a lot of disk activity, so it is more efficient to apply all updates at once rather than at several different times.

If LOB data was opened with the OPEN procedure, it must be closed with the CLOSE procedure before executing a COMMIT statement. If COMMIT is executed when there is still open LOB data, an error will occur. If a ROLLBACK is executed while there is open LOB data, all update operations will be canceled, and all information about the LOB data will be removed.

The following constants are defined in the DBMS_LOB package:

  • LOBMAXSIZE

    The maximum size of LOB data

    LOBMAXSIZE CONSTANT BINARY_INTEGER := 18446744073709551615

    The data type is BINARY_INTEGER and the maximum size of LOB data is 18446744073709551615.

  • LOB_READONLY

    Specifies whether LOB data is read-only

    LOB_READONLY CONSTANT BINARY_INTEGER := 0

    The data type is BINARY_INTEGER. 0 specifies read-only mode.

  • LOB_READWRITE

    Specifies read-write mode for LOB data.

    LOB_READWRITE CONSTANT BINARY_INTEGER := 1

    The data type is BINARY_INTEGER. 1 specifies read-write mode.

  • FILE_READONLY

    Used to open a BFILE.

    FILE_READONLY CONSTANT BINARY_INTEGER := 0

    The data type is BINARY_INTEGER and only read-only mode is allowed.

16.2. Exceptions

The following are the exceptions that are provided by the DBMS_LOB package. For more information, refer to the exception section under “16.3. Procedures” and “16.4. Functions”.

  • INVALID_ARGVAL

  • ACCESS_ERROR

  • NOTEXIST_DIRECTORY

  • NOPRIV_DIRECTORY

  • INVALID_DIRECTORY

  • OPERATION_FAILED

  • UNOPENED_FILE

  • OPEN_TOOMANY

16.3. Procedures

This section describes the procedures provided by the DBMS_LOB package, in alphabetical order.

16.3.1. APPEND

Appends the entire source LOB to the end of the target LOB.

Details about the APPEND procedure are as follows:

  • Prototype

    • BLOB type

      DBMS_LOB.APPEND
      (
          dest_lob            IN OUT NOCOPY       BLOB, 
          src_lob             IN                  BLOB
      );
    • CLOB type

      DBMS_LOB.APPEND
      (
           dest_lob            IN OUT NOCOPY       CLOB, 
           src_lob             IN                  CLOB
      );

      When transmitting CLOB data, LOB data in the dest_lob and src_lob parameters must use the same character set.

  • Parameter

    ParameterDescription
    dest_lobTarget LOB.
    src_lobSource LOB.
  • Exception

    ExceptionDescription
    VALUE_ERROROccurs when the target or source LOB is null.
  • Example

    DECLARE
        dest_lob CLOB := 'All''s fair in ';
        src_lob CLOB := 'love and war';
    BEGIN
        DBMS_LOB.APPEND(dest_lob, src_lob);
        DBMS_OUTPUT.PUT_LINE('Result = ' || dest_lob);
    END;
    /
    Result = All's fair in love and war
    
    PSM completed
    SQL> 

16.3.2. COPY

Copies all or part of the source LOB to the target LOB. The offsets of the source and target LOB can be specified. If the offset of the target LOB is shorter than the length of the target LOB, the source data will overwrite any existing data after the offset.

Reversely, if the offset of the target LOB is longer than the length of the target LOB, any additional spaces will be filled with zeros for BLOB data or blank spaces for CLOB data.

Details about the COPY procedure are as follows:

  • Prototype

    • BLOB type

      DBMS_LOB.COPY
      (
          dest_lob        IN OUT NOCOPY   BLOB,
          src_lob         IN              BLOB,
          amount          IN              INTEGER,
          dest_offset     IN              INTEGER := 1,
          src_offset      IN              INTEGER := 1
      );
    • CLOB type

      DBMS_LOB.COPY
      (
          dest_lob        IN OUT NOCOPY   CLOB,
          src_lob         IN              CLOB,
          amount          IN              INTEGER,
          dest_offset     IN              INTEGER := 1,
          src_offset      IN              INTEGER := 1
      );

      When transmitting CLOB data, LOB data in the dest_lob and src_lob parameters must use the same character set.

  • Parameter

    ParameterDescription
    dest_lobTarget LOB.
    src_lobSource LOB.
    amountNumber of bytes (BLOBs) or characters (CLOBs) to copy.
    dest_offset

    Offset in the target LOB, in bytes for BLOBs or in characters for CLOBs.

    src_offsetOffset in the source LOB, in bytes for BLOBs or in characters for CLOBs.
  • Exception

    ExceptionDescription
    VALUE_ERROROccurs when one or more input parameters are null.
    INVALID_POSOccurs when the value of src_offset or dest_offset is less than 1 or greater than LOBMAXSIZE.
    INVALID_LENOccurs when the value of amount is less than 1 or greater than LOBMAXSIZE.
  • Example

    DECLARE
        dest_lob CLOB := 'It you would be loved, ';
        src_lob CLOB := 'be worthy to be loved';
    BEGIN
        DBMS_LOB.COPY(dest_lob, src_lob, length(src_lob), 
                      length(dest_lob) + 1, 1);
        DBMS_OUTPUT.PUT_LINE('Result = ' || dest_lob);
    END;
    /
    Result = It you would be loved, be worthy to be loved
    
    PSM completed
    SQL> 

16.3.3. CONVERTTOBLOB

Converts a CLOB data to BOLB data.

Details about the CONVERTTOBLOB procedure are as follows:

  • Prototype

    DBMS_LOB.CONVERTTOBLOB
    (
        dest_lob        IN OUT NO COPY  BLOB,
        src_lob         IN              CLOB,
        amount          IN              INTEGER,
        dest_offset     IN OUT          INTEGER,
        src_offset      IN OUT          INTEGER,
        blob_csid       IN              NUMBER,
        lang_context    IN OUT          INTEGER,
        warning         OUT             INTEGER
    );
  • Parameter

    ParameterDescription
    dest_lobTarget LOB locator.
    src_lobSource LOB locator.
    amountNumber of characters to copy.
    dest_offset

    Offset of the target LOB data.

    Output value is the last address where the target LOB data was saved.

    (Unit: byte (BLOB data))

    src_offset

    Offset of the source LOB data.

    Output value is the last address where the source LOB data was read.

    (Unit: character (CLOB data))

    blob_csidID of the character set used to save the converted BLOB data.
    lang_contextUnused.
    warningUnused.
  • Exception

    ExceptionDescription
    VALUE_ERROROccurs when one or more input parameters are NULL.
  • Example

    declare
        src_clob                clob;
        dest_blob               blob;
        dest_clob               clob;
    
        amount                  integer;
        dest_offset             integer;
        src_offset              integer;
        blob_csid               integer;
        lang_context            integer;
        warning                 integer;
    
    begin
        src_clob := 'abcdefghijklmn';
        dbms_lob.createtemporary(dest_blob, false);
        dbms_lob.createtemporary(dest_clob, false);
    
        amount := 10;
        dest_offset := 1;
        src_offset := 1;
        blob_csid := 0;
        lang_context := 0;
        warning := 0;
    
        dbms_lob.CONVERTTOBLOB(dest_blob, src_clob, amount,
                               dest_offset, src_offset, blob_csid,
    			   lang_context, warning);
    end;
    /
    
    PSM completed
    SQL> 

16.3.4. CONVERTTOCLOB

Converts a BLOB data to COLB data.

Details about the CONVERTTOCLOB procedure are as follows:

  • Prototype

    DBMS_LOB.CONVERTTOCLOB
    (
        dest_lob        IN OUT NO COPY  CLOB,
        src_lob         IN              BLOB,
        amount          IN              INTEGER,
        dest_offset     IN OUT          INTEGER,
        src_offset      IN OUT          INTEGER,
        blob_csid       IN              NUMBER,
        lang_context    IN OUT          INTEGER,
        warning         OUT             INTEGER
    );
  • Parameter

    ParameterDescription
    dest_lobTarget LOB locator.
    src_lobSource LOB locator.
    amountNumber of characters to copy.
    dest_offset

    Offset in the target LOB data.

    Output value is the last address where the target LOB data was saved.

    (Unit: byte (CLOB data))

    src_offset

    Offset in the source LOB data.

    Output value is the last address where the source LOB data was read.

    (Unit: character (BLOB data))

    blob_csidID of a character set used to save the converted CLOB data.
    lang_contextUnused.
    warningUnused.
  • Exception

    ExceptionDescription
    VALUE_ERROROccurs when one or more input parameters are NULL.
  • Example

    declare
        src_clob                clob;
        dest_blob               blob;
        dest_clob               clob;
    
        amount                  integer;
        dest_offset             integer;
        src_offset              integer;
        blob_csid               integer;
        lang_context            integer;
        warning                 integer;
    
    begin
        src_clob := 'abcdefghijklmn';
        dbms_lob.createtemporary(dest_blob, false);
        dbms_lob.createtemporary(dest_clob, false);
    
        amount := 10;
        dest_offset := 1;
        src_offset := 1;
        blob_csid := 0;
        lang_context := 0;
        warning := 0;
    
        dbms_output.put_line('org clob : ' || src_clob);
    
        dbms_lob.CONVERTTOBLOB(dest_blob, src_clob, amount,
                               dest_offset, src_offset, blob_csid,
                               lang_context, warning);
    
        dbms_output.put_line('dest offset : ' || dest_offset);
        dbms_output.put_line('srct offset : ' || src_offset);
    
        amount := 20;
        dest_offset := 1;
        src_offset := 1;
        blob_csid := 0;
        lang_context := 0;
        warning := 0;
    
        dbms_lob.CONVERTTOCLOB(dest_clob, dest_blob, amount,
                               dest_offset, src_offset, blob_csid,
                               lang_context, warning);
    
        dbms_output.put_line('dest clob : ' || dest_clob);
        dbms_output.put_line('dest offset : ' || dest_offset);
        dbms_output.put_line('srct offset : ' || src_offset);
    end;
    /
    
    org clob : abcdefghijklmn
    dest offset : 11
    srct offset : 11
    dest clob : abcdefghij
    dest offset : 11
    srct offset : 11
    
    PSM completed.
    
    SQL> 

16.3.5. CREATETEMPORARY

Creates a temporary CLOB or BLOB.

Details about the CREATETEMPORARY procedure are as follows:

  • Prototype

    • BLOB type

      DBMS_LOB.CREATETEMPORARY
      (
          lob         IN OUT NOCOPY   BLOB,
          cache       IN              BOOLEAN,
          dur         IN              PLS_INTEGER := 10
      );
    • CLOB type

      DBMS_LOB.CREATETEMPORARY
      (
          lob         IN OUT NOCOPY   CLOB,
          cache       IN              BOOLEAN,
          dur         IN              PLS_INTEGER := 10
      );
  • Parameter

    ParameterDescription
    lobTarget LOB data.
    cacheOption to save LOB data in the buffer cache.
    durThis parameter does not currently support adjusting the lifetime of the CREATETEMPORARY procedure. By default, the CREATETEMPORARY procedure is automatically deleted after the session ends.
  • Exception

    ExceptionDescription
    VALUE_ERROROccurs when the cache parameter is NULL.
  • Example

    DECLARE
        lob_1 CLOB;
        lob_2 CLOB := 'tibero';
    BEGIN
        DBMS_LOB.CREATETEMPORARY(lob_1, false);
        DBMS_LOB.APPEND(lob_1, lob_2);
        DBMS_OUTPUT.PUT_LINE(lob_1);
    END;
    /
    tibero
    
    PSM completed
    SQL>  

16.3.6. ERASE

Deletes all or part of a LOB. The deleted space is padded with zeros (BLOBs) or blank spaces (CLOBs). If the target LOB is shorter than the sum of the sizes of the offset and the space to be deleted, the size of the actual deleted space can be smaller than the specified size. The actual size of the deleted space is stored in the amount parameter.

Details about the ERASE procedure are as follows:

  • Prototype

    • BLOB type

      DBMS_LOB.ERASE
      (
          lob         IN OUT NOCOPY   BLOB,
          amount      IN OUT NOCOPY   INTEGER,
          offset      IN              INTEGER := 1
      );
    • CLOB type

      DBMS_LOB.ERASE
      (
          lob         IN OUT NOCOPY   CLOB,
          amount      IN OUT NOCOPY   INTEGER,
          offset      IN              INTEGER := 1
      );
  • Parameter

    ParameterDescription
    lobTarget LOB.
    amountNumber of bytes (BLOBs) or characters (CLOBs) to delete.
    offset

    Offset in bytes (BLOBs) or characters (CLOBs) from which to delete.

  • Exception

    ExceptionDescription
    VALUE_ERROROccurs when one or more input parameters is null.
    INVALID_POSOccurs when the value of offset is less than 1 or greater than LOBMAXSIZE.
    INVALID_LENOccurs when the value of amount is less than 1 or greater than LOBMAXSIZE.
  • Example

    DECLARE
        lob CLOB := 'Tmaxsoft Tibero';
        amount NUMBER := 7;
    BEGIN
        DBMS_OUTPUT.PUT_LINE('Length of original LOB = ' || length(lob));
        DBMS_LOB.ERASE(lob, amount, 9);
        DBMS_OUTPUT.PUT_LINE('Value of erased LOB = ' || lob);
        DBMS_OUTPUT.PUT_LINE('Length of erased LOB = ' || length(lob));
    END;
    /
    Length of original LOB = 15
    Value of erased LOB = Tmaxsoft
    Length of erased LOB = 15
    
    PSM completed
    SQL> 

16.3.7. FILECLOSE

Closes a file using the file locator.

Details about the FILECLOSE procedure are as follows:

  • Prototype

    DBMS_LOB.FILECLOSE
    (   
        file_loc     IN OUT NOCOPY   BFILE 
    );
  • Parameter

    ParameterDescription
    file_locFile locator of the target file to close.
  • Exception

    ExceptionDescription
    VALUE_ERROROccurs when the file locator is NULL.
  • Example

    Refer to the FILEOPEN example.

16.3.8. FILECLOSEALL

Closes all open files using the DBMS_LOB package in the current session.

Details about the FILECLOSEALL are as follows.

  • Prototype

    DBMS_LOB.FILECLOSEALL;
  • Example

    BEGIN 
        DBMS_LOB.FILECLOSEALL;
    END;
    /
    

16.3.9. FILEGETNAME

Gets the name of the file and its directory alias from the file locator.

Details about the FILEGETNAME are as follows:

  • Prototype

    DBMS_LOB.FILEGETNAME
    (   
        file_loc     IN   BFILE,
        dir_alias    OUT  VARCHAR2,                                                                       
        filename     OUT  VARCHAR2   
    );
  • Parameter

    ParameterDescription
    file_locFile locator.
    dir_aliasDirectory alias read from the file locator.
    filenameFile name read from the file locator.
  • Exception

    ExceptionDescription
    VALUE_ERROROccurs when the file locator is NULL.
  • Example

    declare
        f1 bfile;
        dir_alias varchar(128);
        filename varchar(128);
    begin
       f1 := bfilename('BFILETESTDIR', 'test001.bin');
       dbms_lob.filegetname(f1, dir_alias, filename); 
       dbms_output.put_line(dir_alias);
       dbms_output.put_line(filename);
    end;
    / 
    
    BFILETESTDIR
    test001.bin 

16.3.10. FILEOPEN

Opens a file as read-only binary mode using the file locator.

Details about the FILEOPEN are as follows:

  • Prototype

    DBMS_LOB.FILEOPEN
    (   
        file_loc     IN OUT NOCOPY   BFILE,
        open_mode    IN              BINARY_INTEGER := file_readonly
    );
  • Parameter

    ParameterDescription
    file_locFile locator of the target file.
    open_modeOnly file_readonly is allowed.
  • Exception

    ExceptionDescription
    VALUE_ERROROccurs when the file locator is NULL.
    INVALID_ARGVALOccurs when open_mode is set to a value other than file_readonly.
    INVALID_DIRECTORYOccurs when the directory specified in the file locator is invalid.
    NOEXIST_DIRECTORYOccurs when the directory path specified in the file locator does not exist.
    OPERATION_FAILEDOccurs when the file does not exist in the directory path or cannot be opened.
  • Example

    create directory BFILETESTDIR as '/mybasepath/bfiletest/';
    
    declare 
        f utl_file.file_type; 
    
        buffer raw(16) := '000102030405060708090A0B0C0D0E0F';
    begin 
        f := utl_file.fopen('BFILETESTDIR', 'test001.bin', 'wb');
        utl_file.put_raw(f, buffer);
        utl_file.fclose(f);
    end;
    /
    
    declare
        f1 bfile;
        amount number;
        buffer raw(16);
        result raw(16) := '000102030405060708090A0B0C0C0E0F';
    begin
       f1 := bfilename('BFILETESTDIR', 'test001.bin');
    
       dbms_lob.fileopen(f1, dbms_lob.file_readonly);
    
       amount := 16;
       dbms_lob.read(f1, amount, 1, buffer);
    
       dbms_output.put_line(amount);
       dbms_output.put_line(buffer); 
        
       dbms_lob.fileclose(f1); 
    end;
    / 
    
    16
    000102030405060708090A0B0C0D0E0F

16.3.11. FREETEMPORARY

Deletes a previously created temporary BLOB or CLOB.

Details about the FREETEMPORARY procedure are as follows:

  • Prototype

    • BLOB type

      DBMS_LOB.FREETEMPORARY
      (
          lob         IN OUT NOCOPY   BLOB
      );
    • CLOB type

      DBMS_LOB.FREETEMPORARY
      (
          lob         IN OUT NOCOPY   CLOB
      );
  • Parameter

    ParameterDescription
    lobTarget LOB.
  • Exception

    ExceptionDescription
    VALUE_ERROROccurs when the input parameter is NULL.
  • Example

    DECLARE
        lob CLOB;
    BEGIN
        DBMS_LOB.CREATETEMPORARY(lob, false);
        DBMS_LOB.FREETEMPORARY(lob);
    END;
    /
    
    PSM completed
    SQL> 

16.3.12. LOADFROMFILE

Writes the data from a file to a LOB. The data file must be open, and the LOB must have been created. Character set conversion is not performed in this procedure. (The CLOB file encoding must be UCS2 as CLOB data is stored as UCS2. Otherwise, this procedure will not function correctly).

Details about the LOADFROMFILE procedure are as follows:

  • Prototypes

    • BLOB

      DBMS_LOB.LOADFROMFILE
      (
          dest_lob    IN OUT NOCOPY BLOB,
          src_file    IN            BFILE,
          amount      IN            INTEGER,
          dest_offset IN            INTEGER := 1,
          src_offset  IN            INTEGER := 1
      );
    • CLOB

      DBMS_LOB.LOADFROMFILE
      (
          dest_lob    IN OUT NOCOPY CLOB,
          src_file    IN            BFILE,
          amount      IN            INTEGER,
          dest_offset IN            INTEGER := 1,
          src_offset  IN            INTEGER := 1
      );
  • Parameters

    ParameterDescription
    dest_lobLOB locator.
    src_file

    File handler of the bfile type that will read in LOB data.

    No character set conversion is performed for BLOB and CLOB version (For CLOB version, only UCS2 encoding is supported for the input text file).

    amount

    Data size to read in.

    Unit is in bytes for BLOB, and character count for CLOB (A character is 2 bytes for UCS2 encoding).

    dest_offsetLOB offset. (Default value: 1, base: 1)
    src_offsetFile offset. (Default value: 1, base: 1)
  • Exceptions

    ExceptionsDescription
    VALUE_ERROROccurs when an input parameter value is invalid.
  • Example

    SQL> create directory BFILETESTDIR as '/home/tests/test_bfile/';
    
    Directory 'BFILETESTDIR' created.
    
    SQL> DECLARE 
        f UTL_FILE.FILE_TYPE; 
    
        buffer RAW(16) := '000102030405060708090A0B0C0C0E0F';
    begin 
        f := UTL_FILE.FOPEN('BFILETESTDIR', 'test001.bin', 'wb');
        UTL_FILE.PUT_RAW(f, buffer);
        UTL_FILE.FCLOSE(f);
    end;
    /
    
    PSM completed.
    
    SQL> SET SERVEROUTPUT ON 
    
    SQL> DECLARE 
        f1 BFILE;
        bl1 BLOB := HEXTORAW('AAAAAA');
        amount NUMBER;
        buffer RAW(16);
        result RAW(16) := '000102030405060708090A0B0C0C0E0F';
        buffer2 RAW(32);
        result2 RAW(32) := '000102030405060708090A0B0C0D0E0F';
    begin
       f1 := BFILENAME('BFILETESTDIR', 'test001.bin');
    
       DBMS_LOB.FILEOPEN(f1, DBMS_LOB.FILE_READONLY);
    
       amount := 16;
       DBMS_LOB.LOADFROMFILE(bl1, f1, amount);
       DBMS_LOB.READ(bl1, amount, 1, buffer);
    
       DBMS_OUTPUT.PUT_LINE(amount);
       DBMS_LOB.FILECLOSE(f1);
    end;
    /
    16
    
    PSM completed.
    
    SQL> DECLARE 
        lob CLOB;
    BEGIN
        DBMS_LOB.CREATETEMPORARY(lob, false);
        DBMS_LOB.FREETEMPORARY(lob);
    END;
    /
    
    PSM completed.
    
    SQL> DECLARE
        f UTL_FILE.FILE_TYPE; 
    
        buffer RAW(16) := '00610062006300640065006600670068';
    BEGIN 
        f := UTL_FILE.FOPEN('BFILETESTDIR', 'ucs2_file.bin', 'wb');
        UTL_FILE.PUT_RAW(f, buffer);
        UTL_FILE.FCLOSE(f);
    END;
    /
    
    PSM completed.
    
    SQL> SET SERVEROUTPUT ON 
    SQL> DECLARE
        f1 BFILE;
        bl1 CLOB := 'init'; 
        amount NUMBER;
    BEGIN
       F1 := BFILENAME('BFILETESTDIR', 'ucs2_file.bin');
    
       DBMS_LOB.FILEOPEN(f1, DBMS_LOB.FILE_READONLY);
    
       AMOUNT := 8;
       DBMS_LOB.LOADFROMFILE(bl1, f1, amount);
    
       DBMS_OUTPUT.PUT_LINE (bl1);
    
       DBMS_LOB.FILECLOSE(f1);
    END;
    /
    abcdefgh
    
    PSM completed.
    
    SQL> 

16.3.13. LOADBLOBFROMFILE

Writes the data from a file to a BLOB. The data file must be open, and the BLOB must have been created.

Details about the LOADBLOBFROMFILE procedure is as follows:

  • Prototype

    DBMS_LOB.LOADBLOBFROMFILE
    (
        dest_lob    IN OUT NOCOPY BLOB,
        src_file    IN            BFILE,
        amount      IN            INTEGER,
        dest_offset IN OUT        INTEGER,
        src_offset  IN OUT        INTEGER
    );
  • Parameters

    ParameterDescription
    dest_lobBLOB locator.
    src_fileFile handler of the bfile type that will read in BLOB data. No character set conversion.
    amountData size (in bytes) to read in.
    dest_offsetBLOB offset (base: 1). After the procedure execution, the offset is changed to the last written position.
    src_offsetFile offset (base: 1). After the procedure execution, the offset is changed to the last read position.
  • Exceptions

    ExceptionsDescription
    VALUE_ERROROccurs when an input parameter value is invalid.
  • Example

    SQL> SET SERVEROUTPUT ON  
    
    SQL> create directory BFILETESTDIR as '/home/tests/test_bfile/';
    
    Directory 'BFILETESTDIR' created.
    
    SQL> DECLARE 
        f UTL_FILE.FILE_TYPE; 
    
        buffer RAW(16) := '000102030405060708090A0B0C0C0E0F';
    begin 
        f := UTL_FILE.FOPEN('BFILETESTDIR', 'test001.bin', 'wb');
        UTL_FILE.PUT_RAW(f, buffer);
        UTL_FILE.FCLOSE(f);
    end;
    /
    
    PSM completed.
    
    SQL> DECLARE 
        f1 BFILE;
        bl1 BLOB := HEXTORAW('AAAAAA');
        amount NUMBER;
        buffer RAW(16);
        result RAW(16) := '000102030405060708090A0B0C0C0E0F';
        buffer2 RAW(32);
        result2 RAW(32) := '000102030405060708090A0B0C0D0E0F';
        src_ofst NUMBER;
        dst_ofst NUMBER;
    begin
       f1 := BFILENAME('BFILETESTDIR', 'test001.bin');
    
       DBMS_LOB.FILEOPEN(f1, dbms_lob.file_readonly);
    
       amount := 16;
        src_ofst := 1;
        dst_ofst := 1;
    
       DBMS_LOB.LOADBLOBFROMFILE(bl1, f1, amount, dst_ofst,  src_ofst);
       DBMS_LOB.READ(bl1, amount, 1, buffer);
    
       DBMS_OUTPUT.PUT_LINE(amount);
       DBMS_OUTPUT.PUT_LINE(src_ofst);
       DBMS_OUTPUT.PUT_LINE(dst_ofst);
    
        
       DBMS_LOB.FILECLOSE(f1);
    end;
    /
    16
    17
    17
    
    PSM completed.
    
    SQL> 

16.3.14. OPEN

Opens a file as read-only binary mode using the file locator.

Details about the OPEN procedure are as follows:

  • Prototype

    DBMS_LOB.OPEN
    (   
        file_loc     IN OUT NOCOPY   BFILE,
        open_mode    IN              BINARY_INTEGER := file_readonly
    );
  • Parameters

    ParameterDescription
    file_locFile locator of the target file.
    open_modeOnly file_readonly is allowed.
  • Exceptions

    ExceptionDescription
    VALUE_ERROROccurs when the file locator is NULL.
    INVALID_ARGVALOccurs when open_mode is set to a value other than file_readonly.
    INVALID_DIRECTORYOccurs when the directory specified in the file locator is invalid.
    NOEXIST_DIRECTORYOccurs when the directory path specified in the file locator does not exist.
    OPERATION_FAILEDOccurs when the file does not exist in the directory path or cannot be opened.
  • Example

    create directory BFILETESTDIR as '/mybasepath/bfiletest/';
    
    declare 
        f utl_file.file_type; 
    
        buffer raw(16) := '000102030405060708090A0B0C0D0E0F';
    begin 
        f := utl_file.fopen('BFILETESTDIR', 'test001.bin', 'wb');
        utl_file.put_raw(f, buffer);
        utl_file.fclose(f);
    end;
    /
    
    declare
        f1 bfile;
        amount number;
        buffer raw(16);
        result raw(16) := '000102030405060708090A0B0C0C0E0F';
    begin
       f1 := bfilename('BFILETESTDIR', 'test001.bin');
    
       dbms_lob.open(f1, dbms_lob.file_readonly);
    
       amount := 16;
       dbms_lob.read(f1, amount, 1, buffer);
    
       dbms_output.put_line(amount);
       dbms_output.put_line(buffer); 
        
       dbms_lob.close(f1);
    end;
    / 
    
    16
    000102030405060708090A0B0C0D0E0F

16.3.15. READ

Reads all or part of a LOB and stores it in the buffer of the output parameter. If the sum of the size of the offset and the size of data to be read is larger than the target LOB, and the procedure needs to read past the end of the LOB, the size of the data that has been actually read can be smaller than the size of the data to be read given as the parameter. If so, the size of the actual read data is stored in the parameter amount. If the location of the offset to be read is larger than the target LOB data, a NO_DATA_FOUND exception will occur.

When transmitting data read from the CLOB to the client, the data is automatically converted to the character set of the client. This can result in the displayed data being different from the actual data read from the CLOB.

Details about the READ procedure are as follows:

  • Prototype

    • BLOB type

      DBMS_LOB.READ
      (
           lob         IN              BLOB,
           amount      IN OUT NOCOPY   BINARY_INTEGER,
           offset      IN              INTEGER,
           buffer      OUT             RAW
      );
    • CLOB type

      DBMS_LOB.READ
      (
           lob         IN              CLOB,
           amount      IN OUT NOCOPY   BINARY_INTEGER,
           offset      IN              INTEGER,
           buffer      OUT             VARCHAR2
      );
  • Parameter

    ParameterDescription
    lobTarget LOB.
    amount

    As an input, specifies the amount of data to read. As an output, contains the amount of data that was actually read. The unit is in bytes for BLOBs or in characters for CLOBs.

    offset

    Offset in bytes (BLOBs) or characters (CLOBs) from which to read.

    bufferOutput buffer to store the data.
  • Exception

    ExceptionDescription
    VALUE_ERROROccurs when one or more input parameters are NULL.
    INVALID_POSOccurs when the offset value is less than 1 or greater than LOBMAXSIZE.
    INVALID_LEN
    • Occurs when the value of amount is less than 1 or greater than MAXBUFSIZE.

    • Occurs when the value of amount is greater than the buffer size.

    NO_DATA_FOUNDOccurs when offset is greater than the size of the target LOB.
  • Example

    DECLARE
        lob CLOB := 'TIBERO fighting!!!';
        buffer VARCHAR2(256);
        amount BINARY_INTEGER := 8;
    BEGIN
        DBMS_LOB.READ(lob, amount, 8, buffer);
        DBMS_OUTPUT.PUT_LINE('Value to be read = ' || buffer);
    END;
    /
    Value to be read = fighting
    
    PSM completed
    SQL>

16.3.16. TRIM

Specifies the length of the target LOB. The length is in bytes for BLOBs or in characters for CLOBs. Any data that exceeds the specified length will be deleted.

If executing this procedure with a LOB with a length of 0, no value will be returned.

Details about the TRIM procedure are as follows:

  • Prototype

    • BLOB type

      DBMS_LOB.TRIM
      (
          lob         IN OUT NOCOPY   BLOB, 
          newlen      IN              INTEGER
      );
    • CLOB type

      DBMS_LOB.TRIM
      (
          lob         IN OUT NOCOPY   CLOB, 
          newlen      IN              INTEGER
      );
  • Parameter

    ParameterDescription
    lobTarget LOB.
    newlen

    New LOB length, in bytes for BLOBs or in characters for CLOBs.

  • Exception

    ExceptionDescription
    VALUE_ERROROccurs when one or more input parameters are NULL.
    INVALID_LENOccurs when the value of newlen is less than 0 or greater than LOBMAXSIZE.
  • Example

    DECLARE
        lob CLOB := 'A pity beyond all telling is in the heart of love';
    BEGIN
        DBMS_LOB.TRIM(lob, 25);
        DBMS_OUTPUT.PUT_LINE('Value = ' || lob);
        DBMS_OUTPUT.PUT_LINE('Length = ' || length(lob));
    END;
    /
    Value = A pity beyond all telling
    Length = 25
    
    PSM completed
    SQL>  

16.3.17. WRITE

Saves a specified length of given data to the specified offset location of the target LOB. The existing data in the target location will be deleted. The offset unit is in bytes for BLOBS or in characters for CLOBs.

An exception occurs if the specified size is larger than the actual size of the given data. If the specified size is smaller than the actual size, data will only be saved up to the specified size. If the specified offset is larger than the target LOB, the extra spaces are padded with zeros for BLOBs or blank spaces for CLOBs.

When executing this procedure for CLOB data, the character set of the CLOB data and that of the data to save must be the same. When the client calls this procedure, the character set of the data will be converted to the character set of the CLOB data.

Details about the WRITE procedure are as follows:

  • Prototype

    • BLOB type

      DBMS_LOB.WRITE
      (
          lob             IN OUT NOCOPY   BLOB,
          amount          IN              BINARY_INTEGER,
          offset          IN              INTEGER,
          buffer          IN              RAW
      );
    • CLOB type

      DBMS_LOB.WRITE
      (
          lob             IN OUT NOCOPY   CLOB,
          amount          IN              BINARY_INTEGER,
          offset          IN              INTEGER,
          buffer          IN              VARCHAR
      );
  • Parameter

    ParameterDescription
    lobTarget LOB.
    amount

    Number of bytes (BLOBs) or characters (CLOBs) to save.

    offset

    Offset in bytes (BLOBs) or characters (CLOBs) from which to save.

    bufferData to save.
  • Exception

    ExceptionDescription
    VALUE_ERROROccurs when one or more parameters are NULL.
    INVALID_POSOccurs when the value of offset is less than 1 or greater than LOBMAXSIZE.
    INVALID_LENOccurs when the value of amount is less than 1 or greater than MAXBUFSIZE.
  • Example

    DECLARE
        lob CLOB;
        buffer VARCHAR2(100);
    BEGIN
        DBMS_LOB.CREATETEMPORARY(lob, false);
        buffer := 'Love is friendship set on fire';
        DBMS_LOB.WRITE(lob, length(buffer), 1, buffer);
        DBMS_OUTPUT.PUT_LINE(lob);
    END;
    /
    Love is friendship set on fire
    
    PSM completed
    SQL> 

16.3.18. WRITEAPPEND

Saves a specified length of given data to the end of the target LOB. This has the same effect as using the WRITE procedure with the offset parameter specified as the length of the target LOB. The unit is in bytes for BLOBs or in characters for CLOBs.

If the specified length is longer than the actual size of the given data, an exception occurs. If it is shorter, only the specified length of data is saved.

When this procedure is executed for CLOB data, the character set of the CLOB data must be the same as that of the data to be saved. When the client calls the procedure, data is saved after the character set of the client is converted to that of the CLOB.

Details about the WRITEAPPEND procedure are as follows:

  • Prototype

    • BLOB type

      DBMS_LOB.WRITEAPPEND 
      (
          lob         IN OUT NOCOPY   BLOB,
          amount      IN              BINARY_INTEGER,
          buffer      IN              RAW
      );
    • CLOB type

      DBMS_LOB.WRITEAPPEND 
      (
          lob         IN OUT NOCOPY   CLOB,
          amount      IN              BINARY_INTEGER,
          buffer      IN              VARCHAR2
      );
  • Parameter

    ParameterDescription
    lobTarget LOB.
    amount

    Number of bytes (BLOBs) or characters (CLOBs) to save.

    buffer

    Data to save, in bytes for BLOBs or in characters for CLOBs.

  • Exception

    ExceptionDescription
    VALUE_ERROROccurs when one or more parameters are NULL.
    INVALID_LENOccurs when the value of amount is less than 1 or greater than MAXBUFSIZE.
  • Example

    DECLARE
        lob CLOB := 'Parting is such ';
        buffer VARCHAR2(100) := 'sweet sorrow';
    BEGIN
        DBMS_LOB.WRITEAPPEND(lob, length(buffer), buffer);
        DBMS_OUTPUT.PUT_LINE('Result = ' || lob);
    END;
    /
    Result = Parting is such sweet sorrow
    
    PSM completed
    SQL>

16.4. Functions

This section describes the functions in the DBMS_LOB package, in alphabetical order.

16.4.1. CLOSE

Closes a file using the file locator.

Details about the CLOSE function are as follows:

  • Prototype

    DBMS_LOB.CLOSE
    (   
        file_loc     IN OUT NOCOPY   BFILE 
    );
  • Parameter

    ParameterDescription
    file_locFile locator of the target file to close.
  • Exception

    ExceptionDescription
    VALUE_ERROROccurs when the file locator is NULL.
  • Example

    Refer to the OPEN example.

16.4.2. COMPARE

This function compares two whole LOBs or parts of two LOBs. The type of the LOBs must be the same for comparison.

Details about the COMPARE function are as follows:

  • Prototype

    • BLOB type

      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;
    • CLOB type

      DBMS_LOB.COMPARE
      (
          lob_1           IN          CLOB,
          lob_2           IN          CLOB,
          amount          IN          INTEGER := 4294967295,
          offset_1        IN          INTEGER := 1,
          offset_2        IN          INTEGER := 1
      ) RETURN INTEGER;

      When transferring CLOB data, the LOB data character set of the parameter lob_1 must match that of lob_2.

  • Parameter

    ParameterDescription
    lob_1First LOB for comparison
    lob_2Second LOB for comparison.
    amountNumber of bytes (BLOBs) or characters (CLOBs) to compare.
    offset_1

    Offset in bytes (BLOBs) or characters (CLOBs) on the first LOB for the comparison.

    offset_2Offset in bytes (BLOBs) or characters (CLOBs) on the second LOB for the comparison.
  • Return Value

    ValueDescription
    0Returned when LOB data of lob_1 is the same as that of lob_2.
    N < 0 or N > 0Returned when LOB data of lob_1 is different from that of lob_2.

    NULL

    • Returned when amount is less than 1.

    • Returned when amount is greater than LOBMAXSIZE.

    • Returned when offset_1 or offset_2 is less than 1.

    • Returned when offset_1 or offset_2 is greater than LOBMAXSIZE.

  • Example

    DECLARE
        lob_1 CLOB := 'abcdefgh';
        lob_2 CLOB := 'abcdefgg';
    BEGIN
        IF DBMS_LOB.COMPARE(lob_1, lob_2) = 0 then
            DBMS_OUTPUT.PUT_LINE('LOB_1 equals LOB_2');
        ELSE 
            DBMS_OUTPUT.PUT_LINE('LOB_1 does not equals LOB_2');
        END IF;
    END;
    /
    LOB_1 does not equals LOB_2
    
    PSM completed
    SQL> 

16.4.3. FILEEXISTS

Checks whether the file exists.

Details about the FILEEXISTS function are as follows:

  • Prototype

    DBMS_LOB.FILEEXISTS
    (   
        file_loc     IN OUT NOCOPY   BFILE,
    ) 
    RETURN INTEGER;
  • Parameter

    ParameterDescription
    file_locFile locator of the target file.
  • Exception

    ExceptionDescription
    VALUE_ERROROccurs when the file locator is NULL.
    INVALID_DIRECTORYOccurs when the directory specified in the file locator is invalid.
    NOEXIST_DIRECTORYOccurs when the directory path specified in the file locator does not exist.
  • Example

    create directory BFILETESTDIR as '/mybasepath/bfiletest/';
    
    declare 
        f utl_file.file_type; 
    
        buffer raw(16) := '000102030405060708090A0B0C0D0E0F';
    begin 
        f := utl_file.fopen('BFILETESTDIR', 'test001.bin', 'wb');
        utl_file.put_raw(f, buffer);
        utl_file.fclose(f);
    end;
    /
    
    declare
        f1 bfile;
        f2 bfile;
    begin
       f1 := bfilename('BFILETESTDIR', 'test001.bin');
       f2 := bfilename('BFILETESTDIR', 'test002.bin');
    
       dbms_output.put_line(dbms_lob.fileexists(f1));
       dbms_output.put_line(dbms_lob.fileexists(f2));
    end;
    / 
    
    1
    0

16.4.4. FILEISOPEN

Checks whether the file was opened with the given locator.

Details about the FILEISOPEN function are as follows:

  • Prototype

    DBMS_LOB.FILEISOPEN
    (   
        file_loc     IN OUT NOCOPY   BFILE,
    ) 
    RETURN INTEGER;
  • Parameter

    ParameterDescription
    file_locFile locator.
  • Exception

    ExceptionDescription
    VALUE_ERROROccurs when the file locator is NULL.
  • Example

    create directory BFILETESTDIR as '/mybasepath/bfiletest/';
    
    declare 
        f utl_file.file_type; 
    
        buffer raw(16) := '000102030405060708090A0B0C0D0E0F';
    begin 
        f := utl_file.fopen('BFILETESTDIR', 'test001.bin', 'wb');
        utl_file.put_raw(f, buffer);
        utl_file.fclose(f);
    end;
    /
    
    declare
        f1 bfile;
    begin
       f1 := bfilename('BFILETESTDIR', 'test001.bin');
    
       dbms_lob.open(f1, dbms_lob.file_readonly);
       dbms_output.put_line(dbmb_lob.fileisopen(f1));
       dbms_lob.close(f1);
       dbms_output.put_line(dbmb_lob.fileisopen(f1));
    end;
    / 
    
    1
    0

16.4.5. GETLENGTH

Returns the length of the target LOB. The length is returned in either bytes or characters according to the LOB data type.

Details about the GETLENGTH function are as follows:

  • Prototype

    • BLOB type

      DBMS_LOB.GETLENGTH
      (
          lob             IN          BLOB
      ) RETURN INTEGER;
    • CLOB type

      DBMS_LOB.GETLENGTH
      (
          lob             IN          CLOB
      ) RETURN INTEGER;
  • Parameter

    ParameterDescription
    lobTarget LOB.
  • Return Value

    ValueDescription
    0Returned when the LOB is empty.
    NULLReturned when the lob parameter is NULL.

    Any zeros or blank spaces filled by a previous COPY, ERASE or WRITE operation are also included in the length.

  • Example

    DECLARE
        lob CLOB := 'architecture';
    BEGIN
        DBMS_OUTPUT.PUT_LINE(DBMS_LOB.GETLENGTH(lob));
    END;
    /
    12
    
    PSM completed
    SQL> 

16.4.6. INSTR

Returns the offset for the nth matching occurrence in the target LOB. The search can start at the beginning of the LOB or at a specified position. The target pattern cannot include wildcard characters, such as a percent (%) sign or an underscore (_), that are used with the LIKE operator.

Details about the INSTR function are as follows:

  • Prototype

    • BLOB type

      DBMS_LOB.INSTR
      (
          lob         IN      BLOB,
          pattern     IN      RAW,
          offset      IN      INTEGER := 1,
          nth         IN      INTEGER := 1
      ) RETURN INTEGER;
    • CLOB type

      DBMS_LOB.INSTR
      (
          lob         IN      CLOB,
          pattern     IN      VARCHAR,
          offset      IN      INTEGER := 1,
          nth         IN      INTEGER := 1
      ) RETURN INTEGER;
  • Parameter

    ParameterDescription
    lobTarget LOB.
    patternPattern to match.
    offset

    Offset in bytes (BLOBs) or characters (CLOBs) from which to start the search.

    nth

    Occurrence number starting from 1.

    Must be greater than or equal to 1. If specified as 1, the offset of the first matched pattern will be returned.

  • Return Value

    ValueDescription
    INTEGER

    If a match is found, the offset of the start of the matched pattern is returned.

    If a match is not found, 0 is returned.

    NULL
    • Returned when one or more parameters are NULL.

    • Returned when offset is less than 1 or greater than LOBMAXSIZE.

    • Returned when nth is less than 1 or greater than LOBMAXSIZE.

  • Example

    DECLARE
        lob CLOB := 'Corporate floor';
        result NUMBER;
    BEGIN
        result := DBMS_LOB.INSTR(lob, 'or', 3, 2);
        DBMS_OUTPUT.PUT_LINE('Result offset = ' || result);
    END;
    /
    Result offset = 14
    
    PSM completed
    SQL> 

16.4.7. ISOPEN

Checks whether LOB was opened already with the given locator.

Details about the ISOPEN function are as follows:

  • Prototype

    DBMS_LOB.ISOPEN
    (   
        file_loc     IN OUT NOCOPY   BFILE,
    ) 
    RETURN INTEGER;
  • Parameter

    ParameterDescription
    file_locFile locator.
  • Exception

    ExceptionDescription
    VALUE_ERROROccurs when the file locator is NULL.
  • Example

    create directory BFILETESTDIR as '/mybasepath/bfiletest/';
    
    declare 
        f utl_file.file_type; 
    
        buffer raw(16) := '000102030405060708090A0B0C0D0E0F';
    begin 
        f := utl_file.fopen('BFILETESTDIR', 'test001.bin', 'wb');
        utl_file.put_raw(f, buffer);
        utl_file.fclose(f);
    end;
    /
    
    declare
        f1 bfile;
    begin
       f1 := bfilename('BFILETESTDIR', 'test001.bin');
    
       dbms_lob.open(f1, dbms_lob.file_readonly);
       dbms_output.put_line(dbmb_lob.isopen(f1));
       dbms_lob.close(f1);
       dbms_output.put_line(dbmb_lob.isopen(f1));
    end;
    / 
    
    1
    0

16.4.8. ISTEMPORARY

Checks if the given LOB is temporary or not.

Details about the ISTEMPORARY function are as follows:

  • Prototype

    • BLOB type

      DBMS_LOB.ISTEMPORARY
      (
          lob         IN          BLOB
      ) RETURN INTEGER;
    • CLOB type

      DBMS_LOB.ISTEMPORARY
      (
          lob         IN          CLOB
      ) RETURN INTEGER;
  • Parameter

    ParameterDescription
    lobTarget LOB.
  • Return Value

    ValueDescription
    1Returned if the LOB is temporary.
    0Returned if the LOB is not temporary.
  • Example

    DECLARE
        lob CLOB;
    BEGIN
        DBMS_LOB.CREATETEMPORARY(lob, false);
        IF DBMS_LOB.ISTEMPORARY(lob) = 1THEN
            DBMS_OUTPUT.PUT_LINE('TRUE');
        ELSE
            DBMS_OUTPUT.PUT_LINE('FALSE');
        END IF;
    END;
    /
    TRUE
    
    PSM completed
    SQL> 

16.4.9. SUBSTR

Returns the specified length of data from the target LOB, starting from the specified offset. The maximum length of the returned data is 32,767 bytes. For CLOB data, the maximum number of characters to be returned is 32,767/2. If this function is called on CLOB data, the returned string has the same character set as the CLOB.

This function returns NULL if any argument is NULL or if the value of amount or offset is less than 1 or greater than LOBMAXSIZE.

If this function is called by the client on a CLOB data and the character set of the client is different from that of the CLOB, the character set is automatically converted during data transmission.

Details about the SUBSTR function are as follows:

  • Prototype

    • BLOB type

      DBMS_LOB.SUBSTR
      (
          lob         IN          BLOB,
          amount      IN          INTEGER := 32767,
          offset      IN          INTEGER := 1
      ) RETURN RAW;
    • CLOB type

      DBMS_LOB.SUBSTR
      (
          lob         IN          CLOB,
          amount      IN          INTEGER := 32767,
          offset      IN          INTEGER := 1
      ) RETURN VARCHAR2;
  • Parameter

    ParameterDescription
    lobTarget LOB.
    amount

    Number of bytes (BLOBs) or characters (CLOBs) to read.

    offset

    Offset in bytes (BLOBs) or characters (CLOBs) from which to read.

  • Return Value

    ValueDescription
    RAW/VARCHAR2 dataReturned when the SUBSTR function completes successfully.
    NULL
    • Returned when any input parameter is NULL.

    • Returned when amount is less than 1 or greater than 32,767 bytes.

    • Returned when offset is less than 1 or greater than LOBMAXSIZE.

  • Example

    DECLARE
        lob CLOB := 'Your friend is too young';
        buffer VARCHAR2(100);
    BEGIN
        buffer := DBMS_LOB.SUBSTR(lob, 6, 6);
        DBMS_OUTPUT.PUT_LINE('My favorite word is ' || UPPER(buffer));
    END;
    /
    My favorite word is FRIEND
    
    PSM completed
    SQL>