Oracle SQL to change column type from number to varchar2 while it contains data

后端 未结 4 1178
广开言路
广开言路 2020-12-31 22:27

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

4条回答
  •  太阳男子
    2020-12-31 22:51

    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;
    

    If there was an index on the column you need to re-create it.

    Note that the update will fail if you have numbers in the old column that are greater than 999. If you do, you need to adjust the maximum value for the varchar column

提交回复
热议问题