--
--Table A : 要 Update / Insert 的 Table
-- ON : 表你自訂條件, 用以判斷 Matched 與否
USING ( SELECT AR_WARHS_CREATE_NO, UNIQUE_LOT_NO , PROD_CD, DEL_FLAG,
(NORMAL_QTY + NORMAL_CORR_QTY ) AS GOOD_QTY
FROM TB_IEM100
WHERE PROD_WC_CD = 'M'
AND PROD_DATE LIKE TO_CHAR(SYSDATE,'YYYYMM') || '%' ) B
ON ( /* 在這個條件下 */
A.IO_DATE LIKE TO_CHAR(SYSDATE,'YYYYMM') || '%'
)
/* 若 MATCHED 則 Update */
A.PROD_CD = B.PROD_CD ,
A.GOOD_QTY = B.GOOD_QTY,
A.DEL_FLAG = B.DEL_FLAG
/* 若 NOT MATCHED 則 Insert */
WHEN NOT MATCHED THEN
B.PROD_CD, B.GOOD_QTY, 0, 'I', 'N', SYSDATE, 'ROBERT','A' );
Merge into 詳細介紹
MERGE語句是Oracle9i新增的語法,用來合併UPDATE和INSERT語句。
通過MERGE語句,根據一張表或子查詢的連接條件對另外一張表進行查詢,
連接條件匹配上的進行UPDATE,無法匹配的執行INSERT。
這個語法僅需要一次全表掃瞄就完成了全部工作,執行效率要高於INSERT+UPDATE。
簡單語法說明:
MERGE [INTO [schema .] table [t_alias]
USING [schema .] { table | view | subquery } [t_alias]
ON ( condition )
WHEN MATCHED THEN merge_update_clause
WHEN NOT MATCHED THEN merge_insert_clause;
詳細語法說明:
MERGE INTO [your table-name] [rename your table here]
USING ( [write your query here] )[rename your query-sql and using just like a table]
ON ([conditional expression here] AND [...]...)
WHEN MATHED THEN [here you can execute some update sql or something else ]
WHEN NOT MATHED THEN [execute something else here ! ]
留言列表