Oracle 8i 替换 CLOB 中的换行字符



>我有一个CLOB,我正在尝试根据需要用rn替换换行字符。这是因为我使用 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
您可以

尝试使用 dbms_lob 包。 这是来自老式"问汤姆"页面的示例

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;

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

REPLACE 适用于 VARCHAR2 而不是 CLOB。假设单个 CLOB"文件"有 177 TB(这是最大 CLOB 大小)。甲骨文会怎么做呢?它将在哪里存储结果?

查看程序DBMS_LOB.FRAGMENT_REPLACE

最新更新