DDL 在 Oracle 块内无法按预期工作
我有一个关于在 oracle 块中运行 ddl 脚本的问题。
基本上我想要一些数据迁移的东西。如果列的数据类型发生更改,我想首先创建一个临时列,将源列数据复制到临时列,然后删除源列,将临时列重命名为源,以完成迁移过程。但是当我运行下面的脚本时,它只会创建一个新的临时列并将数据复制到新列,但它永远不会运行最后两个 alter table 命令。谁能找出问题所在吗?谢谢。
create table t (a int);
insert into t values(10);
--change the data type from integer to varchar2(10);
begin
execute immediate 'alter table t modify a varchar2(10)';
exception when others then
begin
execute immediate 'alter table t add b varchar2(10)';
execute immediate 'update t set b = a';
execute immediate 'alter tabe t drop column a';
execute immediate 'alter table t rename column b to a';
exception when others then
dbms_output.put_line('cann''t change column a from integer to varchar2(10) due to error:' || sqlerrm);
end;
end;
/
I have a question about running ddl script in oracle block.
Basically I want to something for data migration. If a column has its data type changed, I want to create a temp column first, copy source column data to temp column, then drop source column, rename temp column to source, to finish migration process. But when I run the script below, it will only create a new temp column and copy data to the new column, but it will never run the last two alter table command. Can anyone figure out what the problem is? Thanks.
create table t (a int);
insert into t values(10);
--change the data type from integer to varchar2(10);
begin
execute immediate 'alter table t modify a varchar2(10)';
exception when others then
begin
execute immediate 'alter table t add b varchar2(10)';
execute immediate 'update t set b = a';
execute immediate 'alter tabe t drop column a';
execute immediate 'alter table t rename column b to a';
exception when others then
dbms_output.put_line('cann''t change column a from integer to varchar2(10) due to error:' || sqlerrm);
end;
end;
/
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您确实看到 drop column alter 命令有拼写错误“alter tabe...”,对吧?如果这是您尝试运行的代码,那么一旦它遇到此语句,您就应该看到错误消息,并且您的过程仅执行前两个执行语句。
You do see the drop column alter command has the typo "alter tabe...", right? If this is the code you're trying to run, as soon as it hits this statement you should see your error message, and your procedure only does the first two execute statements.