s****y 发帖数: 66 | 1 最近開始用Oracle寫一些很基本的query。類似query之前在MS SQL Server和MySql上實
現都沒問題。換到Oracle最頭疼的一個問題就是居然沒有if exists也沒有insert
ignore之類的。一個簡單的insert都沒有辦法避免primary key violation。網上搜了
一下,都說是要在application code這邊catch exception。可是如果我做的是batch
insert,其中任何一個insert造成primary key violation都會讓整個batch失敗。
這麼簡單的一個事居然沒有一個簡單的辦法解決。覺得不可思議啊!
這裡的Oracle高手指點一下怎麼做insert不用擔心primary key violation吧。 |
y****9 发帖数: 144 | 2 不太熟悉MS SQL 和 MySQL, 能不能给几个 test cases to show how they complete
the same tasks?
In Oracle if we do individual insert statements, seems it won't cause the
whole batch fail.
SQL> ho cat temp.sql
insert into t values(1);
insert into t values(1);
insert into t values(2);
commit;
SQL> @temp
1 row created.
insert into t values(1)
*
ERROR at line 1:
ORA-00001: unique constraint (Vxxxx.T_PK) violated
1 row created.
Commit complete.
SQL> select * from t;
ID
----------
1
2
"if exist" is not a SQL, Oracle PL/SQL has that also
"insert ignore" seems not standard SQL either
【在 s****y 的大作中提到】 : 最近開始用Oracle寫一些很基本的query。類似query之前在MS SQL Server和MySql上實 : 現都沒問題。換到Oracle最頭疼的一個問題就是居然沒有if exists也沒有insert : ignore之類的。一個簡單的insert都沒有辦法避免primary key violation。網上搜了 : 一下,都說是要在application code這邊catch exception。可是如果我做的是batch : insert,其中任何一個insert造成primary key violation都會讓整個batch失敗。 : 這麼簡單的一個事居然沒有一個簡單的辦法解決。覺得不可思議啊! : 這裡的Oracle高手指點一下怎麼做insert不用擔心primary key violation吧。
|
B*****g 发帖数: 34098 | 3 http://www.oracle-base.com/articles/10g/dml-error-logging-10gr2
http://asktom.oracle.com/pls/asktom/f?p=100:11:0%3a%3a%3a%3aP11
【在 s****y 的大作中提到】 : 最近開始用Oracle寫一些很基本的query。類似query之前在MS SQL Server和MySql上實 : 現都沒問題。換到Oracle最頭疼的一個問題就是居然沒有if exists也沒有insert : ignore之類的。一個簡單的insert都沒有辦法避免primary key violation。網上搜了 : 一下,都說是要在application code這邊catch exception。可是如果我做的是batch : insert,其中任何一個insert造成primary key violation都會讓整個batch失敗。 : 這麼簡單的一個事居然沒有一個簡單的辦法解決。覺得不可思議啊! : 這裡的Oracle高手指點一下怎麼做insert不用擔心primary key violation吧。
|
g********s 发帖数: 3652 | 4 既然是PK,上面有constraint 保证这个列的值不可重复所以你无法输入重复值的 |
tj 发帖数: 957 | 5 merge ?
【在 s****y 的大作中提到】 : 最近開始用Oracle寫一些很基本的query。類似query之前在MS SQL Server和MySql上實 : 現都沒問題。換到Oracle最頭疼的一個問題就是居然沒有if exists也沒有insert : ignore之類的。一個簡單的insert都沒有辦法避免primary key violation。網上搜了 : 一下,都說是要在application code這邊catch exception。可是如果我做的是batch : insert,其中任何一個insert造成primary key violation都會讓整個batch失敗。 : 這麼簡單的一個事居然沒有一個簡單的辦法解決。覺得不可思議啊! : 這裡的Oracle高手指點一下怎麼做insert不用擔心primary key violation吧。
|
t*********i 发帖数: 217 | 6 北京mm说的这个管用的呀。
如果是insert select, 也可以加一个where clause.
insert into t1
select * from t2
where not exist (select * from t1 where t1.column =t2.column...); |