【问题标题】:inserting uploaded file values in global table在全局表中插入上传的文件值
【发布时间】:2020-04-06 01:22:01
【问题描述】:

客户端上传文本文件 - 在文本文件的每一行(单列)中包含字母数字 ID。我正在做的是,在我的控制器类中,我从文本文件中读取每一行并将它们分隔为逗号分隔值(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 oracle spring-mvc stored-procedures sql-loader


    【解决方案1】:

    看看这是否有帮助。

    过程将逗号分隔的值拆分为行并将它们插入到表中。

    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>
    

    【讨论】:

    • 感谢您的努力/建议。我如何在我的场景中使用 sql loader?你能指导我吗?没用过sql loader
    • 不客气。 SQL*Loader 示例已添加到我的答案中;看看吧。
    • 非常感谢您提供的大力帮助,我会实施这一点,如果一切顺利,我将支持/接受您的回答。感谢您的努力。
    • 没问题。祝你好运!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-11
    • 2020-07-24
    相关资源
    最近更新 更多