2010-10-06 45 views
0

我有一个包含两个数字列的表以及对它们的唯一约束。我想插入一对新的值,除非这个对已经存在。什么是最简单的方法来做到这一点?在Oracle中,我可以执行“插入或更新到TABLE中的值”

如果我做

insert into TABLE values (100,200) 

和一对已经存在我得到一个ORA-00001错误,所以我想不喜欢

insert or update into TABLE values (100,200) 
+2

如果这对值已经存在,你想要更新什么? – Nellius 2010-10-06 13:49:37

+4

[Oracle:如何UPSERT(更新或插入到表中)]的可能重复(http://stackoverflow.com/questions/237327/oracle-how-to-upsert-update-or-insert-into-a -table) – 2010-10-06 13:56:16

+0

@Nellius:没什么,真的。但如果他们不存在,我想插入它们。这些值来自外部来源。我想将它们插入到数据库中,但我不想编写额外的代码,首先检查它们是否已经存在。 – 2010-10-06 13:57:13

回答

7

东西,你可以使用MERGE

1

你可以尝试类似:

insert into table 
select :a, :b from dual 
where not exists (select 1 from table where column1 = :a and column2=:b) 
相关问题