Call now: 252-767-6166  
Oracle Training Oracle Support Development Oracle Apps

 
 Home
 E-mail Us
 Oracle Articles
New Oracle Articles


 Oracle Training
 Oracle Tips

 Oracle Forum
 Class Catalog


 Remote DBA
 Oracle Tuning
 Emergency 911
 RAC Support
 Apps Support
 Analysis
 Design
 Implementation
 Oracle Support


 SQL Tuning
 Security

 Oracle UNIX
 Oracle Linux
 Monitoring
 Remote s
upport
 Remote plans
 Remote
services
 Application Server

 Applications
 Oracle Forms
 Oracle Portal
 App Upgrades
 SQL Server
 Oracle Concepts
 Software Support

 Remote S
upport  
 Development  

 Implementation


 Consulting Staff
 Consulting Prices
 Help Wanted!

 


 Oracle Posters
 Oracle Books

 Oracle Scripts
 Ion
 Excel-DB  

Don Burleson Blog 


 

 

 


 

 

 

 

 

Oracle dbms_lob tips

Oracle Database Tips by Donald Burleson

About Oracle dbms_lob

Oracle provides the DBMS_LOB package which is used to access and manipulate LOB values in both internal or external storage locations.

dbms_lob Routines That Can Modify BLOB, CLOB, And NCLOB Values

 

Routines That Read Or Examine LOB Values in dbms_lob

 

dbms_lob Read-Only Routines Specific To Bfiles

 

dbms_lob Datatypes

Parameters for the DBMS_LOB routines use the datatypes:

dbms_lob examples

The following SQL statements show the rule conditions for the above mentioned rules:

SELECT dbms_lob.substr(rule_condition,dbms_lob.getlength(rule_condition),1) rule_condition FROM dba_rules where rule_name = 'NY270'
/

RULE_CONDITION
--------------------------------------------------|
((:dml.get_object_owner () = 'NY2') and :dml.is_nul
l_tag() = 'Y' and :dml.get_source_database_name ()

= 'DNYTST10.WORLD' )


SELECT
dbms_lob.substr(rule_condition,dbms_lob.getlength(rule_condition),1)
rule_condition
FROM dba_rules where rule_name = 'NY271'
/


GRAPHICS_DBA
BEGIN dbms_lob.read (:1, :2, :3, :4); END;2121 1 0 10251 488

 

Below is a list containing some of the main procedures and functions that are presented in this package and what they do.

 

n  isopen:  This function checks to see if the LOB was already opened using the input locator.

n  createtemporary:  The procedure createtemporary creates a temporary CLOB or BLOB and its corresponding index in the user default temporary tablespace.

n  instr:  Used to return the matching position of the nth occurrence of the pattern in the LOB.

n  getlength:  Used to get the length of specified LOB.

n  copy: Copies part or all of a source internal LOB to a destination internal LOB.

n  writeappend:  Writes a specified amount of data to the end of an internal LOB.

n  trim:  Trims the value of the internal LOB to the length specified by the newlen parameter.

 

Here is how to perform, search and replace in a CLOB with some procedures of the dbms_lob package. First, create the test table and insert some rows.

 

<  Code   2.4 - dbms_lob.sql

Conn pkg/pkg#123

 

Connected to Oracle 11g Enterprise Edition Release 11.1.0.6.0

Connected as pkg

 

drop table

   tab_dbms_lob_search

purge

/

 

Table dropped

 

create table tab_dbms_lob_search(

   clob_id number,

   c clob

);

 

Table created

 

insert into

   tab_dbms_lob_search

values

   1,'Oracle Database 7i,8i,9i and 10g')

/

 

1 row inserted

insert into

   tab_dbms_lob_search

values

   (2,'Oracle Database 7i,8i,9i and 10g')

/

 

1 row inserted

 

commit

/

 

Commit complete

 

Let's create a procedure that uses the dbms_lob package to search and replace text within a LOB (CLOB, BLOB).

 

< Code   2.5 - search_n_replace_text_lob.sql


create or replace procedure
   proc_dbms_lob_search_rep(

lob_local   in out clob,

        srch_string varchar2,

        rep_string  varchar2) as

temp_clob      clob;

end_offset     integer := 1;

start_offset   integer := 1;

nth      number := 1;

rep_string_len number := length(rep_string);

temp_clob_len  number := 0;

lob_local_len  number := 0;

  

begin

     if lob_local is NULL then

       raise_application_error(-20001, 'LOB is empty. You need to execute inserts first!');

     end if;

  

 

     --The function isopen check to see if the LOB was already opened using the input locator

     if dbms_lob.isopen(

           lob_local) = 0

     dbms_output.put_line(' LOB is open!');

 

     then

       null;

     dbms_output.put_line(' LOB is closed!');

 

     end if;

  

     --The procedure create temporary creates a temporary CLOB

     dbms_lob.createtemporary(

        temp_clob,

        TRUE,

        dbms_lob.session);

LOOP

       -- The function instr returns the matching position of the nth     occurrence of

       --the pattern in the LOB, starting from the offset you specify.

 

       end_offset := dbms_lob.instr(lob_local, srch_string, 1, nth);

       if end_offset = 0

   then

         temp_clob_len := dbms_lob.getlength(temp_clob);

         lob_local_len := dbms_lob.getlength(lob_local) - start_offset + 1;

       

         if lob_local_len > 0

         then

         --The procedure copy copies all, or a part of, a source internal LOB to a destination internal LOB.

           dbms_lob.copy(

              temp_clob,

              lob_local,

              lob_local_len,

              temp_clob_len + 1,

              start_offset);

         end if;

         exit;

       end if;

       --The function getlength gets the length of the specified LOB.

       temp_clob_len := dbms_lob.getlength(temp_clob);

       if (end_offset - start_offset) > 0

       then

         dbms_lob.copy(

            temp_clob,

            lob_local,

            (end_offset - start_offset),

            temp_clob_len + 1,

            start_offset);

       end if;

       start_offset := end_offset + length(srch_string);

       nth    := nth + 1;

  

       if rep_string is not null then

     

   --The procedure writeappend writes a specified amount of data to the end of an internal LOB.

 

         dbms_lob.writeappend(

            temp_clob,

            rep_string_len,

            rep_string);

       end if;

     end loop;

     if length(srch_string) > length(rep_string) then

       --The procedure trim trims the value of the internal LOB to the length you specify in the

       --newlen parameter

       dbms_lob.trim(

          lob_loc => lob_local,

          newlen  =>  dbms_lob.getlength(

                        temp_clob)

       );

     end if;

     dbms_lob.copy(

        lob_local,

        temp_clob,

        dbms_lob.getlength(temp_clob), 1, 1);

   end;

  /

 

Procedure created

 

--Check for errors

show errors

 

No errors for procedure pkg.proc_dbms_lob_search_rep

 

This procedure executed the following steps: 

 

1.       First, it finds the position of the first occurrence of the searched text.

 

2.       Then it creates a temporary NCLOB, copies everything from the original NCLOB to it up to the point where it found the text to be replaced, and appends the new text.

 

3.       Now, it trims the temporary NCLOB to keep only what came after the word to replace.

 

Now let's look at the table results to see how they are before the search and replace process.

 

select

   *

from

   tab_dbms_lob_search;

 

   CLOB_ID C

---------- ---------------------------------------------------------

         1 Oracle Database 7i,8i,9i and 10g

         2 Oracle Database 7i,8i,9i and 10g

 

Call the procedure to change the value for the first row only (clob_id=1).

 


declare

      lob_local clob;

    begin

      select c

         into lob_local

      from

tab_dbms_lob_search

      where

clob_id = 1

      for update;

      proc_dbms_lob_search_rep(

         lob_local   => lob_local,

         srch_string =>  ' and 10g',

         rep_string  =>  ' ,10g and 11g ');

     commit;

   end;

 /

 

The results below show the new replaced text.

 


select

   *

from

   tab_dbms_lob_search

/

 

CLOB_ID   C

------ ----------------------------------------------------------

         1 Oracle Database 7i,8i,9i ,10g and 11g

         2 Oracle Database 7i,8i,9i and 10g

 

In the preceding example, the following procedures and functions were used:  isopen, createtemporary, instr, getlength, copy and trim.

 

 

See these related notes on Oracle dbms_lob

Oracle_dbms_lob.writeappend to join BLOB columns

Oracle_datapump dbms_lob.filegetname

dbms_lob.converttoclob Tips

dbms_lob.getlength gives incorrect length

dbms_lob.getlength tips

 

For more dbms_lob tips see:

Advanced Oracle Utilities: The Definitive Reference by Rampant TechPress is written by top Oracle database experts (Bert Scalzo,  Donald Burleson, and Steve Callan). 

Buy direct from the publisher and save 30%!

 

 

��  
 
 
Oracle Training at Sea
 
 
 
 
oracle dba poster
 

 
Follow us on Twitter 
 
Oracle performance tuning software 
 
Oracle Linux poster
 
 
 

 

Burleson is the American Team

Note: This Oracle documentation was created as a support and Oracle training reference for use by our DBA performance tuning consulting professionals.  Feel free to ask questions on our Oracle forum.

Verify experience! Anyone considering using the services of an Oracle support expert should independently investigate their credentials and experience, and not rely on advertisements and self-proclaimed expertise. All legitimate Oracle experts publish their Oracle qualifications.

Errata?  Oracle technology is changing and we strive to update our BC Oracle support information.  If you find an error or have a suggestion for improving our content, we would appreciate your feedback.  Just  e-mail:  

and include the URL for the page.


                    









Burleson Consulting

The Oracle of Database Support

Oracle Performance Tuning

Remote DBA Services


 

Copyright © 1996 -  2020

All rights reserved by Burleson

Oracle ® is the registered trademark of Oracle Corporation.