2016-03-08 170 views
0

如果我有一个table_abc。现在我改变,并在此添加几列像sql中的alter table脚本

alter table_Abc add (congif_id number, sso number); 

现在我第二次与congif_id沿增加几列和SSO这样的:

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

但这是抛出错误column already exists.

不应该的即使名称相同,alter script仍会运行并添加新脚本?

+0

你怎么能添加同一列两次? –

+0

不,你添加了一次,为什么再次添加它只是删除已经创建一次的列 –

+0

其我们重新运行的脚本。 –

回答

1

不,这个错误是可以预料的。如果必要的话,你可以让你的DDL脚本重新运行的使用动态SQL,例如:

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; 
... 

或者,如果你做这样的事情很多:

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;