2012-10-09 251 views
2
INSERT INTO MAPPING_TBL (G_ID, MR_ID, G_TYPE, G_NUMBER) 
SELECT :G_ID AS G_ID, :MR_ID AS MR_ID, :G_TYPE AS G_TYPE, :G_NUMBER AS G_NUMBER 
FROM DUAL WHERE NOT EXISTS 
     (SELECT G_ID, MR_ID, G_TYPE, G_NUMBER 
      FROM MAPPING_TBL 
      WHERE G_ID = :G_ID2 AND 
       MR_ID = :MR_ID2 AND 
       G_TYPE = :G_TYPE2 AND 
       G_NUMBER = :G_NUMBER2) 

任何人都可以解釋這是什麼實際的SQL?特別是這是什麼這是什麼sql意味着

SELECT :G_ID AS G_ID, :MR_ID AS MR_ID, :G_TYPE AS G_TYPE, :G_NUMBER AS G_NUMBER 
FROM DUAL 

SQL做,謝謝。

+2

如果你以可讀的方式格式化它可能會有所幫助。 – Kevin

回答

3

這是一個「條件插入」。

該聲明是將用戶提供的記錄插入表中MAPPING_TBL如果該記錄不存在於表中。在MySQL中,這是作爲'INSERT IGNORE'完成的。在當代的Oracle中,人們可以使用'MERGE INTO'來達到同樣的效果。

SELECT :G_ID AS G_ID ... FROM DUAL是一種指定值的元組的方法,在這種情況下,調用程序的參數化值。 (例如,如果您選擇了1,2,3,4 FROM DUAL,則會將這些值重新排成一行,在這種情況下,調用程序在查詢執行時提供:G_ID等。)

+0

明白了,謝謝。 – user1730361

0

這只是語句INSERT INTO...SELECT的語法。爲了使它更簡單,採取下列

INSERT INTO table1 (colA, colB, colC) 
SELECT colA, colB, colC 
FROM table2 

這個例子中它的作用是,對不同於table2SELECT版無論行會INSERT版上table1

See for more info: Oracle [INSERT INTO...SELECT]

0
SELECT :G_ID AS G_ID, :MR_ID AS MR_ID, :G_TYPE AS G_TYPE, :G_NUMBER AS G_NUMBER 
FROM DUAL 

分號意味着用戶將被提示輸入這些值。它們就像預計在運行時填充的參數