ORACLE
語法
MERGE [hint] INTO [schema
USING [schema
ON ( condition )
WHEN MATCHED THEN merge_update_clause
WHEN NOT MATCHED THEN merge_insert_clause;
創建測試數據表
create table tj_test(id number
向表中插入數據
insert into tj_test values (
insert into tj_test values (
insert into tj_test values (
select * from tj_test;
查詢結果如下
創建另一新表
create table tj_test
插入一條數據
insert into tj_test
select * from tj_test
查詢結果如下
使用MERGE
merge into tj_test
using tj_test tt
on (tt
when matched then
update set
tt
tt
when not matched then
insert values(
tt
tt
tt
查詢tj_test
select * from tj_test
改變行數據如下
如果存在就更新
如果是單條數據記錄
語法為
MERGE INTO table
USING data_source
ON (condition)
WHEN MATCHED THEN update_clause
WHEN NOT MATCHED THEN insert_clause;
如:
MERGE INTO course c
USING (SELECT course_name
course_hours
FROM course_updates) cu
ON (urse_name = urse_name
AND c
WHEN MATCHED THEN
UPDATE
SET urse_hours = urse_hours
WHEN NOT MATCHED THEN
INSERT (urse_name
urse_hours)
VALUES (urse_name
urse_hours);
From:http://tw.wingwit.com/Article/program/Oracle/201311/18285.html