1

\r我有一个 CLOB,我正在尝试用或\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
4

2 回答 2

2

您可以尝试使用 dbms_lob 包。这是来自古老的 Ask Tom 页面的示例

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;

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

于 2013-10-02T11:04:45.573 回答
0

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

看程序DBMS_LOB.FRAGMENT_REPLACE

于 2013-10-02T10:51:57.300 回答