从 Oracle 表中提取 300,000 个字符的 CLOB 到 CSV

Posted

技术标签:

【中文标题】从 Oracle 表中提取 300,000 个字符的 CLOB 到 CSV【英文标题】:Extract CLOB with 300,000 characters from Oracle Table to CSV 【发布时间】:2021-08-01 11:29:20 【问题描述】:

需要将 Oracle CLOB 从表中提取到用管道分隔的 CSV,但遇到了 32700 个字符的限制。该值包含超过 300,000 个字符。另外,我们如何用空格替换新行。我尝试了 regexp_replace 但它再次有 32700 限制..

任何建议。

谢谢

【问题讨论】:

使用 DBMS_LOB 包并创建 API 以将 CLOB 卸载到 CSV。 DBMS_LOB 文档:docs.oracle.com/database/121/TTPLP/d_lob.htm#TTPLP66624 如果您能给出您尝试过的代码sn-p,我们可能会提供更好的帮助。我们已经做了类似的事情,我们做了 BLOB --> B64/CLOB --> SPOOL Via SQL*PLUS 这是针对特定 CLOB 的一次性练习吗?还是您经常想做的事情?您是否需要可以由客户端程序调用的东西或从 SQLPlus 或您使用的任何工具运行的东西?你想把文件写在哪里?数据库服务器文件系统或您的本地桌面?另外,*“我们如何用空格替换新行”?诶?您的 CLOB 包含换行符,但您想要一个只有一行值的 CSV 文件? 【参考方案1】:

我们如何用空格替换新行。我尝试了regexp_replace,但它又有 32700 的限制。

只需使用REPLACE function,当输入为CLOB 时,输出为CLOB

REPLACE 返回char,每次出现的search_string 都替换为replacement_string。如果replacement_string 被省略或为空,则所有出现的search_string 都将被删除。如果search_string 为空,则返回char

search_stringreplacement_string 以及char 都可以是CHARVARCHAR2NCHARNVARCHAR2CLOBNCLOB 的任何数据类型。返回的字符串与char 在同一字符集中。如果第一个参数不是 LOB,则该函数返回 VARCHAR2,如果第一个参数是 LOB,则返回 CLOB

对于样本数据:

CREATE TABLE table_name ( value CLOB );

DECLARE
  p_value CLOB := EMPTY_CLOB();
BEGIN
  FOR i IN 1 .. 20 LOOP
    p_value := p_value || LPAD( '123456789' || CHR(10), 4000, '123456789|' );
  END LOOP;
  
  INSERT INTO table_name ( value ) VALUES ( p_value );
END;
/

然后查询:

SELECT LENGTH( REPLACE( value, CHR(10), ' ' ) ) AS len,
       REPLACE( value, CHR(10), ' ' ) AS value
FROM   table_name

输出:

LEN   VALUE
40000 123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789 123456789|123456789|123456789|123456789|12345<snip...>56789|123456789 123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789|123456789 

之后,只需为您用于访问数据库的应用程序以正常方式提取 CLOB。

db小提琴here

【讨论】:

以上是关于从 Oracle 表中提取 300,000 个字符的 CLOB 到 CSV的主要内容,如果未能解决你的问题,请参考以下文章

避免多次插入到oracle表中[关闭]

从文件中提取包含大量字符串之一的行

我想在oracle数据库中插入十亿条记录

如何从Oracle SQL中的2个表中提取每个ID的最新记录[重复]

关于使用啥 pl-sql 从 Oracle 10G 数据库中提取大量记录(六百万)的任何建议,并从多个表中进行连接? [复制]

我可以使用 Python 从空的 Access 表中提取架构信息吗?