MODIFY COLUMN in oracle - How to check if a column is nullable before setting to nullable?

后端 未结 2 1831
囚心锁ツ
囚心锁ツ 2020-12-16 09:37

I\'m trying to fill in for a colleague in doing some Oracle work, and ran into a snag. In attempting to write a script to modify a column to nullable, I ran into the lovely

2条回答
  •  不知归路
    2020-12-16 09:55

    You could do this in PL/SQL:

    declare
      l_nullable user_tab_columns.nullable%type;
    begin
      select nullable into l_nullable
      from user_tab_columns
      where table_name = 'MYTABLE'
      and   column_name = 'MYCOLUMN';
    
      if l_nullable = 'N' then
        execute immediate 'alter table mytable modify (mycolumn null)';
      end if;
    end;
    

提交回复
热议问题