Oracle 8i替换CLOB中的换行符

时间:2013-10-02 10:17:55

标签: sql oracle clob oracle8i

我有一个CLOB,我正在尝试使用\r\n替换适当的换行符。这是因为我使用SQL * Plus导出数据并导出换行原始意味着我的解析器查看输出的空白格式以确定字段值不起作用。

我尝试了这样的命令:

SELECT REPLACE(DESCRIPTION, chr(10), '\n') FROM ORDESCRIPTION;

但我明白了:

ORA-00932: inconsistent datatypes

数据:

SQL> select DESCRIPTION from ORDESCRIPTION where or_id = 'FOO/BAR/OR_000002';

DESCRIPTION                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     
--------------------------------------------------------------------------------                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
1)  Why don't you take this and shove it


SQL> spool off

该字段实际上包含更多数据,稍微好一点的数据库浏览器GUI显示:

1)  Why don't you take this and shove it

2)  This section is particularly crap

3)   Do something

2 个答案:

答案 0 :(得分:1)

您可以尝试使用dbms_lob包。这是good old Ask Tom page

的示例
create table ORDESCRIPTION(
  or_id varchar2(255),
  DESCRIPTION clob
);
/

create or replace function lob_replace( p_lob in clob,
                                       p_what in varchar2,
                                       p_with in varchar2 )
return clob
as
    n        number;
    l_offset number := 1;
    l_lob    clob;        
    begin
    dbms_lob.createtemporary( l_lob, TRUE, dbms_lob.session ); 
    dbms_lob.copy( l_lob, p_lob, dbms_lob.getlength(p_lob), 1, 1 );
    loop
        n := dbms_lob.instr( l_lob, p_what, l_offset );
        if ( nvl(n,0) > 0 )
        then
            if ( (n+length(p_what)) < dbms_lob.getlength(l_lob) )
            then
               dbms_lob.copy( l_lob,
                              l_lob,
                              dbms_lob.getlength(l_lob),
                              n+length(p_with),
                              n+length(p_what) );
            end if;

            dbms_lob.write( l_lob, length(p_with), n, p_with );
            if ( length(p_what) > length(p_with) )
            then
                dbms_lob.trim( l_lob,
                   dbms_lob.getlength(l_lob)-(length(p_what)-length(p_with)) );
            end if;
            l_offset := l_offset + length(p_with);
        else
            exit;
        end if;
    end loop;
    return l_lob;
end;
/

insert into ORDESCRIPTION (or_id, description) values
('id001', 'hello'||chr(10)||chr(10)||'world'||chr(10));
/



declare
l_clob clob;
l_clob_replaced clob;
begin
    select DESCRIPTION into l_clob 
      from ORDESCRIPTION 
      where or_id = 'id001';
    dbms_output.put_line(dbms_lob.substr( l_clob, 255, 1 ));
    dbms_output.put_line('----------------');
    l_clob_replaced := lob_replace( l_clob, chr(10), '\n' );
    dbms_output.put_line(dbms_lob.substr( l_clob_replaced, 255, 1 ));
end;
/

select lob_replace(DESCRIPTION, chr(10), '\n')  
from ORDESCRIPTION;

编辑修复了使用示例中的SELECT EDIT2 在使用示例中将FOR UPDATE添加到SELECT语句中 EDIT3 更新了源代码,创建了一个SQLFiddle http://sqlfiddle.com/#!4/8a274/4

答案 1 :(得分:0)

REPLACE适用于VARCHAR2而不适用于CLOB。想象一下,单个CLOB“文件”具有177 TB(最大CLOB大小)。甲骨文将采取什么措施?它将存储结果的位置?

查看程序DBMS_LOB.FRAGMENT_REPLACE

相关问题