oralce数据库(新增、修改字段)

一、新增字段

declare 
  i_cnt integer;
begin
  select count(*) 
  into i_cnt 
  from user_tab_columns ut   where ut.TABLE_NAME = '表名' and ut.COLUMN_NAME = '字段名';  
  if i_cnt = 0 then 
    execute immediate  ' alter table 表名 add  字段名  NUMBER(8) default 0 not null --或者 varchar2(20)';
  end if;   
end;
/

二、修改字段

DECLARE
  I INT;
BEGIN
  SELECT COUNT(*)
    INTO I
    FROM USER_TAB_COLUMNS A
   WHERE A.TABLE_NAME = '表名'
     AND A.COLUMN_NAME = '字段名';
  IF I > 0 THEN
    EXECUTE IMMEDIATE 'alter table 表名 modify 字段名 VARCHAR2(500)';
  END IF;
END;
/

猜你喜欢

转载自blog.csdn.net/qq_16939219/article/details/93466943