2016-03-08 171 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;