插入 TARGET_TABLE SELECT * FROM SOURCE_TABLE;
我想做一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 Oracle 中,您可以使用此 SQL 查询获取公共列:
然后使用上述查询迭代游标,以生成所有返回值的逗号分隔列表。将逗号分隔的字符串放入名为
common_fields
的varchar2
变量中。然后,您可以:In Oracle, You can get common columns with this SQL query:
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 namedcommon_fields
. Then, you can: