天天看點

Oracle修改字段類型(精度)

一、如果表中沒有資料可以直接修改:

--聲明變量存儲要查詢的表中的列是否存在
declare columnExistedCount number;
begin
    --從系統表中查詢表中的列是否存在
    select count(1) into columnExistedCount from user_tab_columns t where t.table_name = upper('表名') and t.column_name = upper('字段名');  
	 if columnExistedCount = 1 then
      execute immediate
      'alter table 表名 modify (字段名 NUMBER(26,4))';
    end if;
end;
           

二、表中有資料修改字段類型:

--1.備份原來的表

create table 表名_BAK as select * from 表名;

--2.删除原來表的資料

delete  from 表名;

--3.修改精度
--聲明變量存儲要查詢的表中的列是否存在
declare columnExistedCount number;
begin
    --從系統表中查詢表中的列是否存在
    select count(1) into columnExistedCount from user_tab_columns t where t.table_name = upper('表名') and t.column_name = upper('字段名');  
	 if columnExistedCount = 1 then
      execute immediate
      'alter table 表名 modify (字段名 NUMBER(26,4))';
    end if;
end;

--4.恢複資料

insert into 表名 select * from 表名_BAK;

--5.删除表
drop table 表名_BAK;