插入 TARGET_TABLE SELECT * FROM SOURCE_TABLE;

发布于 2024-09-29 23:27:04 字数 560 浏览 1 评论 0原文

我想做一个 INSERT / SELECT,这意味着在 TARGET_TABLE 中插入 SOURCE_TABLE 的记录,假设:

SOURCE 和 TARGET 表只有公共列的子集,这意味着在示例中:

==> SOURCE TABLE 有 ALPHA、BETA 和 GAMMA 列;
==>目标表有 BETA、GAMMA 和 DELTA 列。

考虑到并非所有目标列都存在于源表中的假设,生成 INSERT / SELECT 语句的最有效方法是什么?

这个想法是,PL/SQL 脚本检查源表和目标表中的列,进行交集,然后生成具有正确列列表的动态 SQL。

请假设目标表中存在但源表中不存在的列必须保留为 NULL。

我希望将 SOURCE 中的数据提取到一组 INSERT 语句中,以便稍后插入到 TARGET 表中。

您可以假设 TARGET 表的列数比 SOURCE 表的列数多,并且所有SOURCE 表中的列以相同的顺序出现在 TARGET 表中。

预先感谢您提供的有用建议!

I would like to do an INSERT / SELECT, this means INSERT in the TARGET_TABLE the records of the SOURCE_TABLE, with this assumption:

The SOURCE and the TARGET table have only a SUBSET of common columns, this means in example:

==> The SOURCE TABLE has ALPHA, BETA and GAMMA columns;
==> The TARGET TABLE has BETA, GAMMA and DELTA columns.

What is the most efficient way to produce INSERT / SELECT statements, respecting the assumption that not all the target columns are present in the source table?

The idea is that the PL/SQL script CHECKS the columns in the source table and in the target table, makes the INTERSECTION, and then produces a dynamic SQL with the correct list of columns.

Please assume that the columns present in the target table, but not present in the source table, have to be left NULL.

I wish to extract the data from SOURCE into a set of INSERT statements for later insertion into the TARGET table.

You can assume that the TARGET table has more columns than the SOURCE table, and that all the columns in the SOURCE table are present in the TARGET table in the same order.

Thank you in advance for your useful suggestions!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

陌路终见情 2024-10-06 23:27:04

在 Oracle 中,您可以使用此 SQL 查询获取公共列:

select column_name
  from user_tab_columns
 where table_name = 'TABLE_1'
intersect
select column_name
  from user_tab_columns
 where table_name = 'TABLE_2'

然后使用上述查询迭代游标,以生成所有返回值的逗号分隔列表。将逗号分隔的字符串放入名为 common_fieldsvarchar2 变量中。然后,您可以:

sql_sentence := 'insert into TABLE_1 (' || 
                 common_fields || 
                 ') select ' || 
                 common_fields ||
                 ' from TABLE_2';
execute immediate sql_sentence;

In Oracle, You can get common columns with this SQL query:

select column_name
  from user_tab_columns
 where table_name = 'TABLE_1'
intersect
select column_name
  from user_tab_columns
 where table_name = 'TABLE_2'

Then you iterate a cursor with the mentioned query to generate a comma separated list of all values returned. Put that comma separated string into a varchar2 variable named common_fields. Then, you can:

sql_sentence := 'insert into TABLE_1 (' || 
                 common_fields || 
                 ') select ' || 
                 common_fields ||
                 ' from TABLE_2';
execute immediate sql_sentence;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文