可以使用Merge语句来完成此操作。具体实现步骤如下:
示例代码如下:
-- 创建表1 CREATE TABLE table1( id INTEGER, name VARCHAR2(20), age INTEGER );
-- 插入表1数据 INSERT INTO table1(id, name, age) VALUES(1, 'Tom', 21); INSERT INTO table1(id, name, age) VALUES(2, 'Lucy', 18); INSERT INTO table1(id, name, age) VALUES(3, 'John', 25);
-- 创建表2 CREATE TABLE table2( id INTEGER, name VARCHAR2(20), age INTEGER );
-- 插入表2数据 INSERT INTO table2(id, name, age) VALUES(1, 'Tom', 21); INSERT INTO table2(id, name, age) VALUES(2, 'Lucy', 19); INSERT INTO table2(id, name, age) VALUES(4, 'Kate', 22);
-- 使用Merge进行比较并更新 MERGE INTO table1 t1 USING table2 t2 ON (t1.id = t2.id) WHEN MATCHED THEN UPDATE SET t1.age = t2.age WHERE t1.age != t2.age;
-- 查询更新后的数据 SELECT * FROM table1;
输出结果为:
ID | NAME | AGE |
---|---|---|
1 | Tom | 21 |
2 | Lucy | 19 |
3 | John | 25 |
其中,age列的数据已经被更新为与table2中一致的值。