原来一直没注意,merge是可以支持delete,只不过必须的是on条件满足,也就是要求系统支持逻辑删除,而非物理删除。
Using the DELETE Clause with MERGE Statements
You may want to cleanse tables while populating or updating them. To do this, you may want to consider using the DELETE
clause in a MERGE
statement, as in the following example:
MERGE USING Product_Changes S
INTO Products D ON (D.PROD_ID = S.PROD_ID)
WHEN MATCHED THEN
UPDATE SET D.PROD_LIST_PRICE =S.PROD_NEW_PRICE, D.PROD_STATUS = S.PROD_NEWSTATUS
DELETE WHERE (D.PROD_STATUS = "OBSOLETE")
WHEN NOT MATCHED THEN
INSERT (PROD_ID, PROD_LIST_PRICE, PROD_STATUS)
VALUES (S.PROD_ID, S.PROD_NEW_PRICE, S.PROD_NEW_STATUS);
Thus when a row is updated in products
, Oracle checks the delete condition D.PROD_STATUS = "OBSOLETE"
, and deletes the row if the condition yields true.
The DELETE
operation is not as same as that of a complete DELETE
statement. Only the rows from the destination of the MERGE
can be deleted. The only rows that are affected by the DELETE
are the ones that are updated by this MERGE
statement. Thus, although a given row of the destination table meets the delete condition, if it does not join under the ON
clause condition, it is not deleted.