看看这是否有帮助。
过程将逗号分隔的值拆分为行并将它们插入到表中。
SQL> create or replace procedure remove_emp (par_empid in clob) is
2 begin
3 insert into my_temp_table (id)
4 select regexp_substr(par_empid, '[^,]+', 1, level)
5 from dual
6 connect by level <= regexp_count(par_empid, ',') + 1;
7 end;
8 /
Procedure created.
测试:
SQL> begin
2 remove_emp('100X,101Y,102Z,103T,104G,105V,106C,107W,108Q');
3 end;
4 /
PL/SQL procedure successfully completed.
SQL> select * From my_temp_table;
ID
------------------------------
100X
101Y
102Z
103T
104G
105V
106C
107W
108Q
9 rows selected.
SQL>
但是,如果我是你,我会完全跳过它。由于您已经有一个包含数据行的文件,请使用
- SQL*Loader 或
- 外部表功能或
- UTL_FILE 包
加载这样的数据。因为,你现在要做的是
- 将行转换为逗号分隔的 loooong 字符串
- 将其传递给过程
- 将该字符串拆分回行
- 将它们插入表格中
很多工作,大部分都是徒劳的。
对于我建议的“新”选项,SQL*Loader 允许您在本地(在您的 PC 上)拥有源文件,而其他两个选项要求文件位于数据库服务器上。无论您选择哪个选项,它都会比您现在正在做的更快。想一想。
SQL*Loader 示例:
控制文件简单;它假定该文件位于我的c:\temp 目录中,其名称为data16.txt。
load data
infile 'c:\temp\data16.txt'
replace
into table my_temp_table
(
id char(30)
)
表格说明:
SQL> desc my_temp_table;
Name Null? Type
----------------------------------------- -------- ----------------------------
ID VARCHAR2(30)
加载会话:
c:\Temp>sqlldr scott/tiger control=test16.ctl log=test16.log
SQL*Loader: Release 11.2.0.2.0 - Production on Pon Tra 6 12:44:34 2020
Copyright (c) 1982, 2009, Oracle and/or its affiliates. All rights reserved.
Commit point reached - logical record count 8
Commit point reached - logical record count 9
结果:
c:\Temp>sqlplus scott/tiger
SQL*Plus: Release 11.2.0.2.0 Production on Pon Tra 6 12:44:42 2020
Copyright (c) 1982, 2014, Oracle. All rights reserved.
Connected to:
Oracle Database 11g Express Edition Release 11.2.0.2.0 - 64bit Production
SQL> select * From my_temp_table;
ID
------------------------------
100X
101Y
102Z
103T
104G
105V
106C
107W
108Q
9 rows selected.
SQL>