简体   繁体   English

在SQL中更改表脚本

[英]alter table script in sql

If i have a table_abc. 如果我有一个table_abc。 Now i alter and add few columns in this like 现在我改变并添加一些像这样的列

alter table_Abc add ( congif_id number, sso number);

Now second time i add few columns along with congif_id and sso like : 现在第二次我添加一些列以及congif_id和sso,例如:

alter table_Abc add ( congif_id number, sso number,name varchar2(100));

but this is throwing error column already exists. 但这引发错误column already exists.

Shouldnt the alter script still run even if the names are same and add the new ones ? 即使名称相同并添加新名称,更改脚本是否仍应运行?

No, that error is to be expected. 不,该错误是可以预期的。 If necessary you can make your DDL script re-runnable using dynamic SQL, for example: 如有必要,可以使用动态SQL重新运行DDL脚本,例如:

begin
   execute immediate
   'alter table table_Abc add ( congif_id number)';
exception
   when others then 
      if sqlcode = -1430 then
         null;
      end if;
end;

begin
   execute immediate
   'alter table table_Abc add ( sso number)';
exception
   when others then 
      if sqlcode = -1430 then
         null;
      end if;
end;
...

Or if you do this sort of thing a lot: 或者,如果您经常做这种事情:

declare
   procedure run_ddl
      ( p_sql varchar2
      , p_ignored_exception integer
      )
   is
   begin
      execute immediate p_sql;
   exception
      when others then 
         if sqlcode = p_ignored_exception then
            null;
         end if;
   end;
begin
   run_ddl ('alter table table_Abc add ( congif_id number)', -1430);
   run_ddl ('alter table table_Abc add ( sso number)', -1430);
end;

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

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