如何使用 PL/SQL 从 Oracle 中的 Clob 列读取 CSV 数据

发布于 2024-12-10 23:22:49 字数 95 浏览 0 评论 0原文

我从过程中获取一个 clob 作为参数,它包含一个 CSV 文件。我需要读取这些数据并将其加载到另一个 Oracle 表中。

有人可以解释一下如何做到这一点吗?

I'm getting a clob as parameter from a procedure and it contains a CSV file. I need to read this data and load it into another Oracle table.

Could someone please explain how to do this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

并安 2024-12-17 23:22:49

据我所知,甲骨文对此没有现成的好东西。一个有希望的候选者是 DBMS_UTILITY.COMMA_TO_TABLE,但它是严重限制特殊任务使其别无选择。所以你必须卷起袖子,自己做。

您的规范有点模糊,但一个选项是 SPLIT 函数:

create table so18t (
  id number,
  csv clob
);

insert all
into so18t values(1,'1,2,3'||chr(10)||
                    '40,5,6'||chr(10)||
                    '700,80,9'||chr(10))
into so18t values(2,'aaa,bbb,ccc'||chr(10)||
                    'ddd,eee,fff'||chr(10)||
                    'ggg,hhh,iii'||chr(10))
select 1 from dual;

declare
  v_lines jh_util.stringlist_t;
  v_values jh_util.stringlist_t;
begin
  for rec in (select * from so18t order by id) loop
    v_lines := jh_util.split(rec.csv, chr(10));
    for i in v_lines.first .. v_lines.last loop
      dbms_output.put_line('line ' || i || ':');
      v_values := jh_util.split(v_lines(i));
      /* Do what you want with the values - I just print them */
      for j in v_values.first .. v_values.last loop
        dbms_output.put_line('v_values(' || j || ') = ' || v_values(j));
      end loop;
    end loop;
  end loop;
end;
/
show errors

打印:

line 1:
v_values(1) = 1
v_values(2) = 2
v_values(3) = 3
line 2:
v_values(1) = 40
v_values(2) = 5
v_values(3) = 6
line 3:
v_values(1) = 700
v_values(2) = 80
v_values(3) = 9
line 1:
v_values(1) = aaa
v_values(2) = bbb
v_values(3) = ccc
line 2:
v_values(1) = ddd
v_values(2) = eee
v_values(3) = fff
line 3:
v_values(1) = ggg
v_values(2) = hhh
v_values(3) = iii

PL/SQL procedure successfully completed.

当然 Oracle 不提供拆分,但 所以有帮助。在上面的例子中,我使用了我自己的。

其他有趣的资源:

AFAIK Oracle has no ready made goodies for this. One promising candidate is DBMS_UTILITY.COMMA_TO_TABLE, but it's heavily limited to a very special task making it no-option. So you have to roll your sleeves and make your own.

Your specification is a bit vague, but one option is a SPLIT function:

create table so18t (
  id number,
  csv clob
);

insert all
into so18t values(1,'1,2,3'||chr(10)||
                    '40,5,6'||chr(10)||
                    '700,80,9'||chr(10))
into so18t values(2,'aaa,bbb,ccc'||chr(10)||
                    'ddd,eee,fff'||chr(10)||
                    'ggg,hhh,iii'||chr(10))
select 1 from dual;

declare
  v_lines jh_util.stringlist_t;
  v_values jh_util.stringlist_t;
begin
  for rec in (select * from so18t order by id) loop
    v_lines := jh_util.split(rec.csv, chr(10));
    for i in v_lines.first .. v_lines.last loop
      dbms_output.put_line('line ' || i || ':');
      v_values := jh_util.split(v_lines(i));
      /* Do what you want with the values - I just print them */
      for j in v_values.first .. v_values.last loop
        dbms_output.put_line('v_values(' || j || ') = ' || v_values(j));
      end loop;
    end loop;
  end loop;
end;
/
show errors

Prints:

line 1:
v_values(1) = 1
v_values(2) = 2
v_values(3) = 3
line 2:
v_values(1) = 40
v_values(2) = 5
v_values(3) = 6
line 3:
v_values(1) = 700
v_values(2) = 80
v_values(3) = 9
line 1:
v_values(1) = aaa
v_values(2) = bbb
v_values(3) = ccc
line 2:
v_values(1) = ddd
v_values(2) = eee
v_values(3) = fff
line 3:
v_values(1) = ggg
v_values(2) = hhh
v_values(3) = iii

PL/SQL procedure successfully completed.

Of cource Oracle doesn't provide split but SO helps. In the example above I have used my own one.

Other interesting resources:

望她远 2024-12-17 23:22:49

不要将数据导出到文件。

您需要将 clob 转换为有用的内容,解析它,然后写入另一个表。以下是您需要执行的步骤:

  1. 将 Clob 从 Clob 转换为有用的东西。 CLOB.getCharacterStream() 似乎很有用。
  2. 从转换后的 Clob 对象中解析 CSV 数据。 CSVReader reader = new CSVReader(the_reader_from_getCharacterStream); ftw
  3. 将所需数据存储在另一个表中。

Oracle 的 CLOB 对象 提供了一些有用的方法。
CSVReader 来自打开 CSV

Do not export the data to a file.

You will need to convert the clob into something useful, parse it, then write to the other table. Here is the steps you need to do:

  1. Convert the Clob from a Clob to a something useful. CLOB.getCharacterStream() seems useful.
  2. Parse the CSV data from the converted Clob object. CSVReader reader = new CSVReader(the_reader_from_getCharacterStream); ftw
  3. Store the desired data in the other table.

Oracle's CLOB Object provides some useful methods.
CSVReader is from Open CSV.

各自安好 2024-12-17 23:22:49

我不知道将 clob 解析为 CSV 的直接方法,但 Oracle 提供了许多用于处理 CSV 文件 的工具,例如 外部表SQL*Loader

因此,一种方法可能是:

  1. 使用 DBMS_LOBUTL_FILE
  2. 使用提到的 CSV 工具之一加载文件多于。

I don't know of an immediate way of parsing a clob as a CSV, but Oracle provides a number of tools for working with CSV files such as External Tables and SQL*Loader.

So an approach might be to:

  1. Export the clob as a file using DBMS_LOB and UTL_FILE
  2. Load the file using one of the CSV tools mentioned above.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文