-1

你能解释一下为什么在执行这个查询时会产生错误 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 行终止。

4

2 回答 2

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 *

此外,直接路径插入是不同的

于 2017-05-24T14:08:58.123 回答
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

顺便说一句,你为什么使用数字中的字符串?

于 2017-05-24T14:08:33.570 回答