你可以使用MERGE語句。除其他外,它允許一個簡單的UPSERT(它實際上允許行的SET的UPSERT而不是單行)。考慮:
SQL> CREATE TABLE TEST (
2 ID NUMBER,
3 a VARCHAR2(10),
4 b VARCHAR2(10),
5 CONSTRAINT pk_test PRIMARY KEY (ID)
6 );
Table created
SQL> MERGE INTO TEST t
2 USING (SELECT 1 ID, 'a' a, 'b' b FROM dual) new_row
3 ON (t.id = new_row.id)
4 WHEN MATCHED THEN
5 UPDATE SET t.a = new_row.a,
6 t.b = new_row.b
7 WHEN NOT MATCHED THEN
8 INSERT (ID, a, b) VALUES (new_row.id, new_row.a, new_row.b);
Done
SQL> SELECT * FROM TEST;
ID A B
---------- ---------- ----------
1 a b
SQL> MERGE INTO TEST t
2 USING (SELECT 1 ID, 'x' a, 'y' b FROM dual) new_row
3 ON (t.id = new_row.id)
4 WHEN MATCHED THEN
5 UPDATE SET t.a = new_row.a,
6 t.b = new_row.b
7 WHEN NOT MATCHED THEN
8 INSERT (ID, a, b) VALUES (new_row.id, new_row.a, new_row.b);
Done
SQL> SELECT * FROM TEST;
ID A B
---------- ---------- ----------
1 x y
i-e:您可以使用相同的語句插入和更新。
乾杯,
-
文森特
這與問題有什麼關係? – 2012-08-04 20:56:06