2017-05-24 43 views
-1

能否請您解釋一下爲什麼ORA-00918,而在行執行這個查詢直接路徑INSERT查詢產生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行。

回答

1

問題在於,您在查詢上使用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是不同的東西

0

我沒有看到任何「Direct-Path」插入。無論如何,嘗試這一個

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 

btw,你爲什麼從數字使用字符串?