每当有一个新会员信息时,则插入一个新的行记录到members
表。 然后,members
表中的数据将与member_staging
表的数据合并。
以下语句用于创建members
和member_staging
表:
CREATE TABLE members (
member_id NUMBER PRIMARY KEY,
first_name VARCHAR2(50) NOT NULL,
last_name VARCHAR2(50) NOT NULL,
rank VARCHAR2(20)
CREATE TABLE member_staging AS
SELECT * FROM members;
使用以下INSERT
语句将示例数据插入到members
和member_staging
表中:
-- insert into members table
INSERT INTO members(member_id, first_name, last_name, rank) VALUES(1,'Abel','Wolf','Gold');
INSERT INTO members(member_id, first_name, last_name, rank) VALUES(2,'Clarita','Franco','Platinum');
INSERT INTO members(member_id, first_name, last_name, rank) VALUES(3,'Darryl','Giles','Silver');
INSERT INTO members(member_id, first_name, last_name, rank) VALUES(4,'Dorthea','Suarez','Silver');
INSERT INTO members(member_id, first_name, last_name, rank) VALUES(5,'Katrina','Wheeler','Silver');
INSERT INTO members(member_id, first_name, last_name, rank) VALUES(6,'Lilian','Garza','Silver');
INSERT INTO members(member_id, first_name, last_name, rank) VALUES(7,'Ossie','Summers','Gold');
INSERT INTO members(member_id, first_name, last_name, rank) VALUES(8,'Paige','Mcfarland','Platinum');
INSERT INTO members(member_id, first_name, last_name, rank) VALUES(9,'Ronna','Britt','Platinum');
INSERT INTO members(member_id, first_name, last_name, rank) VALUES(10,'Tressie','Short','Bronze');
-- insert into member_staging table
INSERT INTO member_staging(member_id, first_name, last_name, rank) VALUES(1,'Abel','Wolf','Silver');
INSERT INTO member_staging(member_id, first_name, last_name, rank) VALUES(2,'Clarita','Franco','Platinum');
INSERT INTO member_staging(member_id, first_name, last_name, rank) VALUES(3,'Darryl','Giles','Bronze');
INSERT INTO member_staging(member_id, first_name, last_name, rank) VALUES(4,'Dorthea','Gate','Gold');
INSERT INTO member_staging(member_id, first_name, last_name, rank) VALUES(5,'Katrina','Wheeler','Silver');
INSERT INTO member_staging(member_id, first_name, last_name, rank) VALUES(6,'Lilian','Stark','Silver');
将members
表中的数据更新到member_staging
表时,应该执行以下操作:
更新成员id
为1
,3
,4
和6
的行记录,因为表中这些成员的排名或姓氏是不同的。 要插入成员id
为7
到10
的行记录,这是因为这些行存在于members
表中,但不存在于member_staging
表中。
总共有8
行数据需要合并。参考以下图示 -
以下是一次性执行所有这些操作的MERGE
语句。
MERGE INTO member_staging x
USING (SELECT member_id, first_name, last_name, rank FROM members) y
ON (x.member_id = y.member_id)
WHEN MATCHED THEN
UPDATE SET x.first_name = y.first_name,
x.last_name = y.last_name,
x.rank = y.rank
WHERE x.first_name <> y.first_name OR
x.last_name <> y.last_name OR
x.rank <> y.rank
WHEN NOT MATCHED THEN
INSERT(x.member_id, x.first_name, x.last_name, x.rank)
VALUES(y.member_id, y.first_name, y.last_name, y.rank);
merge
语句根据member_id
列中的值(参见上面的ON
子句),将member
表中的每一行与member_staging
表中的每一行进行比较。
如果两个表的member_id
列中的值相等,MERGE
语句只有在两个表的first_name
,last_name
或rank
列的不值时,才将members
表中的first_name
,last_name
或rank
列的不值作为member_staging
对应列的值来更新,否则它将members
的行直接插入member_staging
表。
Oracle按照预期返回了8
行合并数据。
在本教程中,您已学习如何使用Oracle MERGE
语句根据指定的条件更新或插入数据。
¥ 我要打赏
纠错/补充