提取CLOB数据以进行插入

时间:2019-07-07 20:22:14

标签: oracle plsql clob

我有如下CLOB数据:

123456 (LED TV); 234543 (LED light); 654876 (LED monitor);

现在,我需要使用定界符(在我的情况下为“ ; ”)从CLOB中提取所有6位数字(在我的情况下为发票跟踪号),然后选择“插入目标表”如果记录不存在。

我已经看到了Instr&Substr或Regexp的几个示例,但是我所需要的或对Oracle的了解都不多。有人可以告诉我一个示例,该示例如何将CLOB基于CLOB内的字符串拆分为行,以在稍后的Insert中使用它吗?

P.S .:我希望采用最快的解决方案,因为我的CLOB数据可能包含500万以上的发票记录。最后将是从C#触发的存储过程,但这部分使我头疼...对于任何帮助-预先感谢!

2 个答案:

答案 0 :(得分:1)

我尝试使用<toggle label="Foo" :active="this.$store.getters.FooStatus">Foo</toggle> 包完成任务,以将其转换为除以“;” 的字符串,然后对其进行一些字符串操作以达到结果。

尝试以下操作:

DBMS_LOB

db<>fiddle demo

干杯!

答案 1 :(得分:0)

这是一个例子。

首先测试用例; test表包含源数据:

SQL> create table test (col clob);

Table created.

SQL> insert into test
  2    select '123456 (LED TV); 234543 (LED light); 654876 (LED monitor);' from dual union all
  3    select '665988 (Notebook); 987654 (Mouse); 445577 (Dead Pixel);'    from dual;

2 rows created.

SQL>

Target表将包含从源中提取的值:

SQL> create table target (itn number, name varchar2(20));

Table created.

SQL> -- This value shouldn't be inserted as it already exists in the TARGET table:
SQL> insert into target values (234543, 'LED light');

1 row created.

SQL>

现在,有用的东西。这个想法是将列值拆分为行(这是分层查询中的regexp_substr部分,然后将ID值与名称(括在方括号中)分开。目标表中存在的值不应插入(因此查询应该插入5行):

SQL> insert into target (itn, name)
  2  with
  3  c2r as
  4    -- split column to rows, e.g. "123456 (LED TV)" is an example of such a row
  5    (select to_char(trim(regexp_substr(col, '[^;]+', 1, column_value))) val
  6     from test join table(cast(multiset(select level from dual
  7                                        connect by level <= regexp_count(col, ';')
  8                                       ) as sys.odcinumberlist)) on 1 = 1
  9    ),
 10  sep as
 11    -- separate ITN (invoice tracking nubmer) and NAME
 12    (select substr(val, 1, instr(val, ' ') - 1) itn,
 13            substr(val, instr(val, ' ') + 1) name
 14     from c2r
 15    )
 16  select s.itn, replace(replace(s.name, '(', ''), ')', '')
 17  from sep s
 18  -- don't insert values that already exist in the TARGET table
 19  where not exists (select null from target t
 20                    where t.itn = s.itn
 21                   );

5 rows created.

SQL>

最终结果:

SQL> select * From target;

       ITN NAME
---------- --------------------
    234543 LED light
    123456 LED TV
    654876 LED monitor
    665988 Notebook
    987654 Mouse
    445577 Dead Pixel

6 rows selected.

SQL>