客户端上传文本文件 - 在文本文件的每一行(单列)中包含字母数字 ID。我正在做的是,在我的 Controller 类中,我从文本文件中读取每一行,并将它们分隔为逗号分隔值(100X,101Y,102Z,103T,104G,105V,106C,107W,108Q)。
我将逗号分隔的值作为单个字符串发送到 oracle 过程,我需要将这些值插入到全局表中(每个值位于不同的行中)。我是存储过程的新手,所以我不知道应该如何在全局表中插入这些值。或者我可以使用sql loader来实现我的要求,我们如何使用sql loader?你们能帮助/指导我解决我的问题吗?请检查下面的代码以获取更多信息。
//just for a test
create global temporary table my_temp_table( //global table
id varchar2(30);
)
on commit preserve rows;
//this is my procedure
CREATE OR REPLACE PROCEDURE remove_emp (
employee_id IN CLOB //CONTAINS COMMA SEPERATED ALPHANUMERIC VALUES ('100X,101Y,102Z,103T'..)
) AS
BEGIN
INSERT INTO my_temp_table(id) VALUES( //not working for me, need help here
employee_id)
最佳答案
看看这是否有帮助。
过程将逗号分隔的值拆分为行并将它们插入到表中。
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 字符串
- 将其传递给过程
- 将该字符串拆分回行
- 将它们插入表格
很多工作,大部分都是徒劳的。
就我建议的"new"选项而言,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)
加载 session :
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>
关于sql - 在全局表中插入上传的文件值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61051639/