相关文章推荐
独立的汉堡包  ·  Using ControlNet ...·  2 年前    · 
闷骚的灌汤包  ·  Python数据分析案例24——基于深度学习 ...·  2 年前    · 
热情的黑框眼镜  ·  sed只修改第一次匹配-掘金·  2 年前    · 
爱喝酒的香瓜  ·  为什么节点服务器必须在文件更改时重新启动? ...·  3 年前    · 
风度翩翩的眼镜  ·  使用org.springframework. ...·  3 年前    · 
小百科  ›  Oracle:插入表错误-重复列名或无效数据类型开发者社区
神勇威武的长颈鹿
2 年前
首页
学习
活动
专区
工具
TVP 最新优惠活动
返回腾讯云官网
提问

问 Oracle:插入表错误-重复列名或无效数据类型

Database Administration用户
提问于 2020-08-10 04:23:01
EN

我正在尝试从外部表中插入数据。

代码语言: javascript
复制
INSERT /*+ ignore_row_on_dupkey_index ( consruct ( construct_id ) ) */
INTO construct 
      (construct_id,
      n_term ,
      enz_name,
      c_term,
      mutations,
      mw_kda)
SELECT *
    FROM EXTERNAL ((
      construct_id NUMBER(10),
      n_term VARCHAR2 (50),
      enz_name VARCHAR2 (50),
      c_term VARCHAR2 (50),
      cpp VARCHAR2 (50),
      mutations VARCHAR2 (50),
      mw_kda NUMBER (7,3))
  TYPE ORACLE_LOADER
    DEFAULT DIRECTORY data_to_input
    ACCESS PARAMETERS (
        RECORDS DELIMITED BY NEWLINE
        SKIP 1
        BADFILE bad_files:'badflie_insert_into_construct_from_construct.bad'
        FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
        MISSING FIELD VALUES ARE NULL 
    LOCATION ('CONSTRUCT.CSV')
    REJECT LIMIT UNLIMITED) ext
    WHERE NOT EXISTS (
        SELECT * FROM construct c
        WHERE c.n_term = ext.n_term
        AND c.enz_name = ext.enz_name
        AND c.c_term = ext.c_term
        AND c.cpp = ext.cpp
        AND c.mutations = ext.mutations
    );

但现在我发现了一个错误:

代码语言: javascript
复制
Error at Command Line : 171 Column : 7
Error report -
SQL Error: ORA-00957: duplicate column name
00957. 00000 -  "duplicate column name"

第171行是本部分的最后一行。

代码语言: javascript
复制
INSERT /*+ ignore_row_on_dupkey_index ( consruct ( construct_id ) ) */
INTO construct 
      (construct_id,
      n_term ,
      enz_name,
      c_term,
      mutations,
      mw_kda)

这显然不是一个重复的列。

如果我这样做了:

代码语言: javascript
复制
INSERT /*+ ignore_row_on_dupkey_index ( consruct ( construct_id ) ) */
INTO construct 
    (construct_id, 
    n_term, 
    enz_name, 
    c_term, 
    mutations, 
    mw_kda)
SELECT *
    FROM EXTERNAL ((
      ext.construct_id NUMBER (10),
      ext.n_term VARCHAR2 (50),
      ext.enz_name VARCHAR2 (50),
      ext.c_term VARCHAR2 (50),
      ext.cpp VARCHAR2 (50),
      ext.mutations VARCHAR2 (50),
      ext.mw_kda NUMBER (7,3))
  TYPE ORACLE_LOADER
    DEFAULT DIRECTORY data_to_input
    ACCESS PARAMETERS (
        RECORDS DELIMITED BY NEWLINE
        SKIP 1
        BADFILE bad_files:'badflie_insert_into_construct_from_construct.bad'
        FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
        MISSING FIELD VALUES ARE NULL 
    LOCATION ('CONSTRUCT.CSV')
    REJECT LIMIT UNLIMITED) ext
    WHERE NOT EXISTS (
        SELECT * FROM construct c
        WHERE c.n_term = ext.n_term
        AND c.enz_name = ext.enz_name
        AND c.c_term = ext.c_term
        AND c.cpp = ext.cpp
        AND c.mutations = ext.mutations
    );

我得到了

代码语言: javascript
复制
Error at Command Line : 174 Column : 10
Error report -
SQL Error: ORA-00902: invalid datatype
00902. 00000 -  "invalid datatype"

第171行是 ext.construct_id NUMBER (10),

1 622 0 票数 1
EN
datatypes
oracle-19c
oracle
insert

回答 1

Database Administration用户

回答已采纳

发布于 2020-08-10 20:02:30

外部表名为 construct_ext

代码语言: javascript
复制
CREATE TABLE construct_ext
  (    construct_id NUMBER (10),
      n_term VARCHAR2 (50),
      enz_name VARCHAR2 (50),
      c_term VARCHAR2 (50),
      cpp VARCHAR2 (50),
      mutations VARCHAR2 (50),
      mw_kda NUMBER (7,3)
  ORGANIZATION EXTERNAL
    TYPE ORACLE_LOADER
    DEFAULT DIRECTORY data_to_input
    ACCESS PARAMETERS 
        RECORDS DELIMITED BY NEWLINE CHARACTERSET US7ASCII 
        TERRITORY AMERICA
        SKIP 1
        BADFILE bad_files:'badflie_insert_into_construct_from_construct.bad'
        LOGFILE bad_files:'logflie_insert_into_construct_from_construct.log'
        FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
        MISSING FIELD VALUES ARE NULL 
    LOCATION ('CONSTRUCT.CSV')
   ) REJECT LIMIT UNLIMITED;
INSERT /*+ ignore_row_on_dupkey_index ( consruct ( construct_id ) ) */
INTO construct 
    (construct_id, 
    n_term, 
    enz_name, 
    c_term, 
    mutations, 
    mw_kda)
SELECT 
      construct_id,
      n_term ,
      enz_name,
      c_term ,
      cpp ,
      mutations ,
      mw_kda
      construct_ext ext
WHERE NOT EXISTS (
        SELECT * FROM construct c
        WHERE c.n_term = ext.n_term
        AND c.enz_name = ext.enz_name
        AND c.c_term = ext.c_term
        AND c.cpp = ext.cpp
        AND c.mutations = ext.mutations
);

编辑:-创建外部表后,验证是否能够查询表

编辑:-根据@a_horse_with_no_name,您可以访问外部表,而无需为数据库版本18c及以上创建 内联外部表

当您尝试插入目标表而不创建外部表(希望有人能说明这一点)时,问题就出现了:创建暂存表,然后插入目标表。

代码语言: javascript
复制
    CREATE TABLE construct_stg  
    -- select part works without creating external table 
    SELECT *
        FROM EXTERNAL (
          construct_id NUMBER(10),
          n_term VARCHAR2 (50),
          enz_name VARCHAR2 (50),
          c_term VARCHAR2 (50),
          cpp VARCHAR2 (50),
          mutations VARCHAR2 (50),
          mw_kda NUMBER (7,3)
        TYPE ORACLE_LOADER
        DEFAULT DIRECTORY data_to_import 
        ACCESS PARAMETERS (
            RECORDS DELIMITED BY NEWLINE CHARACTERSET US7ASCII
            TERRITORY AMERICA
            SKIP 1
            BADFILE bad_files:'badflie_insert_into_construct_from_construct.bad'
            --LOGFILE bad_files:'logflie_insert_into_construct_from_construct.log'--create different directory for log file or skip this line 
            FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
            MISSING FIELD VALUES ARE NULL
              construct_id,
              n_term ,
              enz_name,
              c_term ,
              cpp ,
              mutations ,
              mw_kda 
        LOCATION ('CONSTRUCT.CSV')
        REJECT LIMIT UNLIMITED)ext_tab;
   CREATE INDEX idx_construct_id ON construct_stg(construct_id);
   ALTER TABLE construct_stg ADD CONSTRAINT pk_construct_id_stg PRIMARY KEY (construct_id) USING INDEX;  -- to enforce unique or skip this
    INSERT /*+ ignore_row_on_dupkey_index ( consruct,idx_construct_id) */
    INTO construct 
    (construct_id, 
    n_term, 
    enz_name, 
    c_term, 
    mutations, 
    mw_kda)
SELECT 
      construct_id,
      n_term ,
      enz_name,
      c_term ,
      cpp ,
      mutations ,
      mw_kda
      construct_stg ext
WHERE NOT EXISTS (
        SELECT * FROM construct c
 
推荐文章
独立的汉堡包  ·  Using ControlNet models to remodel my living room | Python-bloggers
2 年前
闷骚的灌汤包  ·  Python数据分析案例24——基于深度学习的锂电池寿命预测 | AI技术聚合
2 年前
热情的黑框眼镜  ·  sed只修改第一次匹配-掘金
2 年前
爱喝酒的香瓜  ·  为什么节点服务器必须在文件更改时重新启动? - 问答 - 腾讯云开发者社区-腾讯云
3 年前
风度翩翩的眼镜  ·  使用org.springframework.mock.web.MockMultipartFile碰见的坑_mockmultipartfile依赖包_前方太黑暗的博客-CSDN博客
3 年前
Link管理   ·   Sov5搜索   ·   小百科
小百科 - 百科知识指南