oracle 如何使用apex数据加载器自动更新导入表中的空列

xuo3flqw  于 2023-06-29  发布在  Oracle
关注(0)|答案(2)|浏览(163)

你知道如何在使用apex数据加载器导入数据后自动更新空列(person_id)的表吗?在我的csv文件模板中,我不包括人员ID,我只包括员工编号。我不喜欢包括在csv文件模板的人id,我希望它能自动更新的情况下,导入的数据有空的人id。

bakd9h0s

bakd9h0s1#

我有两个选择。
第一种方法(如果您的数据库支持)是使用标识列,并让Oracle填充ID值:

SQL> CREATE TABLE so_test
  2  (
  3     id      NUMBER GENERATED BY DEFAULT ON NULL AS IDENTITY,
  4     empno   NUMBER,
  5     ename   VARCHAR2 (50)
  6  );

Table created.

SQL> INSERT INTO so_test (empno, ename) VALUES (1234, 'Littlefoot');

1 row created.

SQL> INSERT INTO so_test (id, empno, ename) VALUES (100, 4443, 'Bigfoot');

1 row created.

SQL> SELECT * FROM so_test;

        ID      EMPNO ENAME
---------- ---------- --------------------------------------------------
         1       1234 Littlefoot      --> ID created by Oracle
       100       4443 Bigfoot         --> ID inserted manually

SQL> DROP TABLE so_test;

Table dropped.

第二种是使用数据库触发器(带序列):

SQL> CREATE TABLE so_test
  2  (
  3     id      NUMBER,
  4     empno   NUMBER,
  5     ename   VARCHAR2 (50)
  6  );

Table created.

SQL> CREATE SEQUENCE seq_so_test;

Sequence created.

SQL> CREATE OR REPLACE TRIGGER trg_bi_so_test
  2     BEFORE INSERT
  3     ON so_test
  4     FOR EACH ROW
  5     WHEN (new.id IS NULL)
  6  BEGIN
  7     :new.id := seq_so_test.NEXTVAL;
  8  END;
  9  /

Trigger created.

SQL> INSERT INTO so_test (empno, ename) VALUES (1234, 'Littlefoot');

1 row created.

SQL> INSERT INTO so_test (id, empno, ename) VALUES (100, 4443, 'Bigfoot');

1 row created.

SQL> SELECT * FROM so_test;

        ID      EMPNO ENAME
---------- ---------- --------------------------------------------------
         1       1234 Littlefoot       --> ID created by a trigger
       100       4443 Bigfoot          --> ID inserted manually

SQL> DROP TABLE so_test;

Table dropped.

SQL>
lvjbypge

lvjbypge2#

您的表是否已经有触发器?如果不是,则创建一个trigger并相应地调整触发器的查询,例如:

IF :NEW.person_id IS NULL THEN
  :NEW.person_id := <sequence_name>.NEXTVAL;
END IF;

相关问题