直接路径INSERT查询生成ORA-00918错误

时间:2017-05-24 14:03:04

标签: oracle ora-00918 direct-path

您能解释为什么在执行此查询时生成错误ORA-00918

INSERT INTO CLG_TEST_2 (CLG_TEST_2.record_id, CLG_TEST_2.chain_id, 

CLG_TEST_2.chain_n, 
CLG_TEST_2.contact_info)

select * from (

SELECT 1, 1, 0, '2222' from dual UNION ALL

SELECT 2, 2, 0, '4444' from dual UNION ALL

SELECT 3, 3, 0, '6666' from dual

)

第1行出错 ORA-00918:列模糊定义

脚本在第2行终止。

2 个答案:

答案 0 :(得分:2)

问题在于,您在查询中使用select *而没有为列提供别名;这将有效:

INSERT INTO CLG_TEST_2 (CLG_TEST_2.record_id,
                        CLG_TEST_2.chain_id,
                        CLG_TEST_2.chain_n,
                        CLG_TEST_2.contact_info)
select *
from (
        SELECT 1 a, 1 b, 0 c, '2222' d from dual UNION ALL
        SELECT 2  , 2  , 0  , '4444'   from dual UNION ALL
        SELECT 3  , 3  , 0  , '6666'   from dual
     )

但是,您可以简化代码:

INSERT INTO CLG_TEST_2 (record_id, chain_id, chain_n, contact_info)
    SELECT 1, 1, 0, '2222' from dual UNION ALL
    SELECT 2, 2, 0, '4444' from dual UNION ALL
    SELECT 3, 3, 0, '6666' from dual

更多关于错误原因的信息。

您的代码:

SQL> INSERT INTO CLG_TEST_2 (
  2      CLG_TEST_2.record_id,
  3      CLG_TEST_2.chain_id,
  4      CLG_TEST_2.chain_n,
  5      CLG_TEST_2.contact_info)
  6  select * from (
  7      SELECT 1, 1, 0, '2222' from dual UNION ALL
  8      SELECT 2, 2, 0, '4444' from dual UNION ALL
  9      SELECT 3, 3, 0, '6666' from dual
 10  );
select * from (
       *
ERROR at line 6:
ORA-00918: column ambiguously defined

略有不同:

SQL> INSERT INTO CLG_TEST_2 (
  2      CLG_TEST_2.record_id,
  3      CLG_TEST_2.chain_id,
  4      CLG_TEST_2.chain_n,
  5      CLG_TEST_2.contact_info)
  6  select * from (
  7      SELECT 1, 2, 0, '2222' from dual UNION ALL
  8      SELECT 2, 2, 0, '4444' from dual UNION ALL
  9      SELECT 3, 3, 0, '6666' from dual
 10  );

3 rows created.

有什么不同?

在第一行,我改变了

SELECT 1, 1, 0, '2222' --> SELECT 1, 2, 0, '2222'
          ^                          ^

原因:

SQL> SELECT 1, 2, 0, '2222' from dual UNION ALL
  2  SELECT 2, 2, 0, '4444' from dual UNION ALL
  3  SELECT 3, 3, 0, '6666' from dual;

         1          2          0 '222
---------- ---------- ---------- ----
         1          2          0 2222
         2          2          0 4444
         3          3          0 6666

SQL> SELECT 1, 1, 0, '2222' from dual UNION ALL
  2  SELECT 2, 2, 0, '4444' from dual UNION ALL
  3  SELECT 3, 3, 0, '6666' from dual;

         1          1          0 '222
---------- ---------- ---------- ----
         1          1          0 2222
         2          2          0 4444
         3          3          0 6666

SQL>

这里有两列具有相同别名'1'的列,这对于外部select *来说很困惑。

此外,direct-path insert是不同的

答案 1 :(得分:0)

我没有看到任何“直接路径”插入。无论如何,试试这个

INSERT INTO CLG_TEST_2 (CLG_TEST_2.record_id, CLG_TEST_2.chain_id,     
   CLG_TEST_2.chain_n, 
   CLG_TEST_2.contact_info)    
SELECT 1, 1, 0, '2222' from dual UNION ALL
SELECT 2, 2, 0, '4444' from dual UNION ALL
SELECT 3, 3, 0, '6666' from dual

顺便问一下,为什么要使用数字中的字符串?