问题描述
我必须阅读备份表和加载到环境中的新表的差异。
如果发现任何行有任何差异,我需要要求用户根据备份表保留/更新/删除该记录。
如何在plsql中编写此需求?
示例表:
创建表backup_test (名称varchar2(10),值varchar2(10));
创建表测试 (名称varchar2(10),值varchar2(10));
如果发现任何行有任何差异,我需要要求用户根据备份表保留/更新/删除该记录。
如何在plsql中编写此需求?
示例表:
创建表backup_test (名称varchar2(10),值varchar2(10));
创建表测试 (名称varchar2(10),值varchar2(10));
专家解答
很大程度上取决于如何表达数据。
例如,如果要对整个列集进行差异,则可以进行减号操作
它很容易扩展其他列的外部连接,例如
SQL> create table t1 as select rownum x1, rownum y1 from dual connect by level <= 10;
Table created.
SQL> create table t2 as select rownum+4 x2, rownum+4 y2 from dual connect by level <= 10;
Table created.
SQL>
SQL> select * from t1;
X1 Y1
---------- ----------
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9
10 10
10 rows selected.
SQL> select * from t2;
X2 Y2
---------- ----------
5 5
6 6
7 7
8 8
9 9
10 10
11 11
12 12
13 13
14 14
10 rows selected.
例如,如果要对整个列集进行差异,则可以进行减号操作
SQL>
SQL>
SQL> ( select * from t1
2 minus
3 select * from t2 )
4 union all
5 ( select * from t2
6 minus
7 select * from t1 ) ;
X1 Y1
---------- ----------
1 1
2 2
3 3
4 4
11 11
12 12
13 13
14 14
8 rows selected.
Or if the tables share some sort of natural key, you could use FULL OUTER JOIN to see which keys are disparate
SQL>
SQL> select * from t1 full outer join t2
2 on t1.x1 = t2.x2
3 where t1.x1 is null or t2.x2 is null;
X1 Y1 X2 Y2
---------- ---------- ---------- ----------
11 11
12 12
13 13
14 14
1 1
2 2
4 4
3 3
8 rows selected.
它很容易扩展其他列的外部连接,例如
SQL> update t1 set y1 = y1*10 where x1 = 8;
1 row updated.
SQL>
SQL> select * from t1 full outer join t2
2 on t1.x1 = t2.x2
3 where t1.x1 is null
4 or t2.x2 is null
5 or decode(t1.y1,t2.y2,1,0) = 0 ;
X1 Y1 X2 Y2
---------- ---------- ---------- ----------
8 80 8 8
11 11
12 12
13 13
14 14
1 1
2 2
4 4
3 3
9 rows selected.
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




