这个sql是什么意思

时间:2012-10-09 01:53:32

标签: sql oracle

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实际做了什么吗?尤其是这个

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

sql do,谢谢。

3 个答案:

答案 0 :(得分:3)

这是“条件插入”。

如果表中尚不存在该记录,则该语句将用户提供的记录插入到表MAPPING_TBL中。在MySQL中,这是作为'INSERT IGNORE'完成的。在当代的Oracle中,人们可以使用“MERGE INTO”来达到同样的效果。

SELECT :G_ID AS G_ID ... FROM DUAL是一种指定值元组的方法,在本例中是来自调用程序的参数化值。 (例如,如果您选择了SELECT 1,2,3,4 FROM DUAL,那么您将获得这些值。在这种情况下,调用程序在查询执行时提供:G_ID等等。)

答案 1 :(得分:0)

它只是INSERT INTO...SELECT语句的语法。为了使其更简单,请采用以下示例

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

它的作用是,SELECT table2 INSERT的所有行都将table1 {{1}} {/ 1}}。

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

答案 2 :(得分:0)

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

分号表示将提示用户输入这些值。它们类似于预期在运行时填充的参数