简体   繁体   English

使用SQL在Oracle DB中迁移数据

[英]Migration of Data in Oracle DB using SQL

I have an issue with migration of data in Oracle DB during some release upgrades. 在某些版本升级期间,Oracle数据库中的数据迁移存在问题。

Case: 案件:

  • Table X in release 1 has three coulmns. 版本1中的表X具有三个结语。
  • Same Table X in release 2 has five columns( two added in release 2 ). 版本2中的相同表X有五列( 版本2中增加了两列)。
  • Same table in release 3 has five columns as in release 2. 版本3中的同一表具有与版本2中相同的五列。
  • Upgrade paths include Release 1 to Release 3 and Release 2 to Release 3 . 升级路径包括版本1到版本3以及版本2到版本3

I need a Oracle SQL query which copies data from a TMP table to actual table in both cases based on coulmns size from TMP where i have stored the data temporarily(this has to be done). 我需要一个Oracle SQL查询,在这两种情况下,都将根据我临时存储数据的TMP的库仑大小将数据从TMP表复制到实际表(必须这样做)。

Below is the query which i tried but it isnt working. 以下是我尝试过的查询,但它不起作用。

insert into USER.X values
(CASE (select count(*) from all_tab_columns where table_name='TMP') 
WHEN '3' THEN (select USER.TMP.*, null NEWCOL1 from USER.TMP, null NEWCOL2 from USER.TMP) 
WHEN '5' THEN (select USER.TMP.* from USER.TMP) 
END
);

Please help in this regard and if there is a better way of doing the same please let me know. 请在这方面提供帮助,如果有更好的方法,请告诉我。

Edit: There are multiple problems in you logic. 编辑:您的逻辑中存在多个问题。

  1. You cannot determine number of parameters to insert statement at runtime. 您无法确定要在运行时插入语句的参数数量。 You have to determine it before creating insert statement. 您必须在创建插入语句之前确定它。
  2. Case returns only 1 value. Case仅返回1个值。 More than that and you will get error too many values 不仅如此,您还会得到错误的too many values

So you should 所以你应该

  • Create stored proc 创建存储的过程
  • Use if else and create insert statement based on that. 使用if else并在此基础上创建插入语句。
  • Execute it by Execute immidiate 通过Execute immidiate

Prev. 上一个 Response 响应

The first problem in your query is that select count(*) from all_tab_columns where table_name='TMP' returns an integer, whereas in case you are comparing it to '3' and '5' as varchar. 查询中的第一个问题是select count(*) from all_tab_columns where table_name='TMP'返回一个整数,而case您将其与varchar比较为'3''5' So assuming that rest of your query return result correctly, try replacing '3' and '5' as 3 and 5 因此,假设查询的其余部分正确返回了结果,请尝试将'3''5'替换为35

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM