7

I have a table (that contains data) in Oracle 11g and I need to use Oracle SQLPlus to do the following:

Target: change the type of column TEST1 in table UDA1 from number to varchar2.

Proposed method:

  1. backup table
  2. set column to null
  3. change data type
  4. restore values

The following didn't work.

create table temp_uda1 AS (select * from UDA1); 

update UDA1 set TEST1 = null;
commit;

alter table UDA1 modify TEST1 varchar2(3);

insert into UDA1(TEST1)
  select cast(TEST1 as varchar2(3)) from temp_uda1;
commit;

There is something to do with indexes (to preserve the order), right?

4

4 に答える 4

30
create table temp_uda1 (test1 integer);
insert into temp_uda1 values (1);

alter table temp_uda1 add (test1_new varchar2(3));

update temp_uda1 
   set test1_new = to_char(test1);

alter table temp_uda1 drop column test1 cascade constraints;
alter table temp_uda1 rename column test1_new to test1;

列にインデックスがあった場合は、再作成する必要があります。

古い列に 999 より大きい数値がある場合、更新は失敗することに注意してください。その場合、varchar列の最大値を調整する必要があります。

于 2013-09-24T09:58:52.550 に答える