Reading clob line by line with pl\sql

后端 未结 9 960
长情又很酷
长情又很酷 2020-12-02 00:53

In my project i use oracle as primary database and i\'ve faced a problem with parsing clob. So suppose we have a clob with value

   aaaaaa
   cccccc
   bbbbb         


        
9条回答
  •  广开言路
    2020-12-02 01:31


    sample for dynamicly length of rows

    and alternative for UNIX and WIN files

    and CR/LF on end of file or without it


    Create table for TEST

    drop table pbrev.test_SVT_tmp;
    create table pbrev.test_SVT_tmp (xc clob);
    insert into pbrev.test_SVT_tmp (xc) values (
    --'azertyuiop;11' || chr(13) || chr(10) ||'qsdfghjklm;7878' || chr(13) || chr(10) ||'wxcvbn;0' || chr(13) || chr(10) );
    'azertyuiop;11' || chr(13) || chr(10) ||'qsdfghjklm;7878' || chr(13) || chr(10) ||'wxcvbn;0' );
    'azerty jhjh  huiop;11
    qsdfgkj  hjklhhhhhhhhhhhm;7878
    wxcvbn;0
    dkjsk kjdsk5456 4654 5646 54645
    FINISH'
    );
    delete from pbrev.test_SVT_tmp ;
    select xc from pbrev.test_SVT_tmp;
    
    --SET SERVEROUTPUT ON;
    --SET SERVEROUTPUT OFF;
    declare
        nStartIndex number := 1;
        nEndIndex number := 1;
        nLineIndex number := 0;
        vLine varchar2(2000);
        cursor c_clob is
        select xc from pbrev.test_SVT_tmp;
        c clob;
        procedure printout
           (p_clob in out nocopy clob) is
          offset number := 1;
          amount number := 32767;
          amount_last number := 0;
          len    number := dbms_lob.getlength(p_clob);
          lc_buffer varchar2(32767);
          line_seq pls_integer := 1;
          -- For UNIX type file - replace CHR(13) to NULL
          CR char := chr(13);
          --CR char := NULL;
          LF char := chr(10);      
          nCRLF number;
          sCRLF varchar2(2);
          b_finish boolean := true;
    begin
          sCRLF := CR || LF;
          nCRLF := Length(sCRLF);
          if ( dbms_lob.isopen(p_clob) != 1 ) then
            dbms_lob.open(p_clob, 0);
          end if;
          amount := instr(p_clob, sCRLF, offset);
          while ( offset < len )
          loop
            -- For without CR/LF on end file
            If amount < 0 then
              amount := len - offset + 1;
              b_finish := false;
            End If;
            dbms_lob.read(p_clob, amount, offset, lc_buffer);
            If b_finish then
              lc_buffer := SUBSTR(lc_buffer,1,Length(lc_buffer)-1);  
            End If;
            if (line_seq-1) > 0 then
              amount_last := amount_last + amount;
              offset := offset + amount; 
            else
              amount_last := amount;
              offset := amount + nCRLF;
            end if;
            amount := instr(p_clob, sCRLF, offset);
            amount := amount - amount_last;
            dbms_output.put_line('Line #'||line_seq||': '||lc_buffer);
            line_seq := line_seq + 1;
          end loop; 
          if ( dbms_lob.isopen(p_clob) = 1 ) then
            dbms_lob.close(p_clob);
          end if; 
        exception
          when others then
             dbms_output.put_line('Error : '||sqlerrm);
        end printout;
    begin
        open c_clob;
        loop
           fetch c_clob into c;
           exit when c_clob%notfound;
           printout(c);
        end loop;
        close c_clob;
    end;
    

提交回复
热议问题