如何复制表中除应更改的单个列之外的所有数据
我有一个关于针对具有不同数据的表的统一插入查询的问题 结构(甲骨文)。让我用一个例子来详细说明:
tb_customers (
id NUMBER(3), name VARCHAR2(40), archive_id NUMBER(3)
)
tb_suppliers (
id NUMBER(3), name VARCHAR2(40), contact VARCHAR2(40), xxx, xxx,
archive_id NUMBER(3)
)
所有表中唯一存在的列是 [archive_id]。该计划是通过将所有记录复制(复制)到不同的数据库分区并相应地增加这些记录的 archive_id 来创建数据集的新存档。 [archive_id] 始终是主键的一部分。
我的问题是使用 select 语句来执行数据的实际复制。由于列是可变的,我正在努力想出一个统一的选择语句来复制数据并更新 archive_id。
一种解决方案(可行)是迭代存储过程中的所有表并执行以下操作:
CREATE TABLE temp as (SELECT * from ORIGINAL_TABLE);
UPDATE temp SET archive_id=something;
INSERT INTO ORIGINAL_TABLE (select * from temp);
DROP TABLE temp;
我不太喜欢这种解决方案,因为 DDL 命令会弄乱所有还原点。
还有其他人有任何解决方案吗?
I have a question regarding a unified insert query against tables with different data
structures (Oracle). Let me elaborate with an example:
tb_customers (
id NUMBER(3), name VARCHAR2(40), archive_id NUMBER(3)
)
tb_suppliers (
id NUMBER(3), name VARCHAR2(40), contact VARCHAR2(40), xxx, xxx,
archive_id NUMBER(3)
)
The only column that is present in all tables is [archive_id]. The plan is to create a new archive of the dataset by copying (duplicating) all records to a different database partition and incrementing the archive_id for those records accordingly. [archive_id] is always part of the primary key.
My problem is with select statements to do the actual duplication of the data. Because the columns are variable, I am struggling to come up with a unified select statement that will copy the data and update the archive_id.
One solution (that works), is to iterate over all the tables in a stored procedure and do a:
CREATE TABLE temp as (SELECT * from ORIGINAL_TABLE);
UPDATE temp SET archive_id=something;
INSERT INTO ORIGINAL_TABLE (select * from temp);
DROP TABLE temp;
I do not like this solution very much as the DDL commands muck up all restore points.
Does anyone else have any solution?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为每个基表创建一个全局临时表怎么样?
您不需要每次都创建和删除它们,只需将它们保持原样即可。
那么您的存档过程就是一个事务...
希望这有帮助。
How about creating a global temporary table for each base table?
You don't need to create and drop these each time, just leave them as-is.
You're archive process is then a single transaction...
Hope this helps.
我建议不要对所有表使用单个 sql 语句,而只使用和插入。
或者,如果您确实需要一个 sql 语句来处理所有这些语句,那么至少要预先创建所有临时表(作为临时表)并将它们保留在适当的位置以供下次使用。然后只需使用动态sql来引用临时表即可。
I would suggest not having a single sql statement for all tables and just use and insert.
Or if you really need a single sql statement for all of them at least precreate all the temp tables (as temp tables) and leave them in place for next time. Then just use dynamic sql to refer to the temp table.