问题描述
嗨,汤姆,
我需要写一个plsql程序,
假设每个表中有10个具有不同列的sup表和1个目标表,这些表的列与合并的所有源列相同。我需要编写一个过程,该过程将从源表中提取数据并转储到目标表中。如果添加了更多的源表,这肯定会在目标表中添加一些新列,这将是未来的收获。代码不应更改。
你能帮我一下吗?
我需要写一个plsql程序,
假设每个表中有10个具有不同列的sup表和1个目标表,这些表的列与合并的所有源列相同。我需要编写一个过程,该过程将从源表中提取数据并转储到目标表中。如果添加了更多的源表,这肯定会在目标表中添加一些新列,这将是未来的收获。代码不应更改。
你能帮我一下吗?
专家解答
对我来说听起来只是一系列的合并。
您可以查询数据字典,以便在添加表时动态生成这些合并命令。
SQL> create table t1 ( pk int, c1 int, c2 int );
Table created.
SQL> create table t2 ( pk int, c3 int, c4 int );
Table created.
SQL>
SQL> insert into t1 select rownum, rownum, rownum from dual connect by level <= 10;
10 rows created.
SQL> insert into t2 select rownum, rownum, rownum from dual connect by level <= 10;
10 rows created.
SQL>
SQL> create table target ( pk int, c1 int, c2 int, c3 int, c4 int );
Table created.
SQL>
SQL> merge into target
2 using ( select * from t1 ) t1
3 on ( target.pk = t1.pk )
4 when matched
5 then update
6 set target.c1 = t1.c1,
7 target.c2 = t1.c2
8 when not matched
9 then insert ( target.pk, target.c1, target.c2 )
10 values ( t1.pk, t1.c1, t1.c2 );
10 rows merged.
SQL>
SQL>
SQL> merge into target
2 using ( select * from t2 ) t2
3 on ( target.pk = t2.pk )
4 when matched
5 then update
6 set target.c3 = t2.c3,
7 target.c4 = t2.c4
8 when not matched
9 then insert ( target.pk, target.c3, target.c4 )
10 values ( t2.pk, t2.c3, t2.c4 );
10 rows merged.
SQL>
SQL> select * from target order by 1;
PK C1 C2 C3 C4
---------- ---------- ---------- ---------- ----------
1 1 1 1 1
2 2 2 2 2
3 3 3 3 3
4 4 4 4 4
5 5 5 5 5
6 6 6 6 6
7 7 7 7 7
8 8 8 8 8
9 9 9 9 9
10 10 10 10 10
10 rows selected.
您可以查询数据字典,以便在添加表时动态生成这些合并命令。
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




