Changing precision of numeric column in Oracle

后端 未结 3 547
轮回少年
轮回少年 2020-12-13 08:34

Currently I have a column that is declared as a NUMBER. I want to change the precision of the column to NUMBER(14,2).

SO, I ran the command

 alter t         


        
相关标签:
3条回答
  • 2020-12-13 09:18

    If the table is compressed this will work:

    alter table EVAPP_FEES add AMOUNT_TEMP NUMBER(14,2);
    
    update EVAPP_FEES set AMOUNT_TEMP = AMOUNT;
    
    update EVAPP_FEES set AMOUNT = null;
    
    alter table EVAPP_FEES modify AMOUNT NUMBER(14,2);
    
    update EVAPP_FEES set AMOUNT = AMOUNT_TEMP;
    
    alter table EVAPP_FEES move nocompress;
    
    alter table EVAPP_FEES drop column AMOUNT_TEMP;
    
    alter table EVAPP_FEES compress;
    
    0 讨论(0)
  • 2020-12-13 09:20

    Assuming that you didn't set a precision initially, it's assumed to be the maximum (38). You're reducing the precision because you're changing it from 38 to 14.

    The easiest way to handle this is to rename the column, copy the data over, then drop the original column:

    alter table EVAPP_FEES rename column AMOUNT to AMOUNT_OLD;
    
    alter table EVAPP_FEES add AMOUNT NUMBER(14,2);
    
    update EVAPP_FEES set AMOUNT = AMOUNT_OLD;
    
    alter table EVAPP_FEES drop column AMOUNT_OLD;
    

    If you really want to retain the column ordering, you can move the data twice instead:

    alter table EVAPP_FEES add AMOUNT_TEMP NUMBER(14,2);
    
    update EVAPP_FEES set AMOUNT_TEMP = AMOUNT;
    
    update EVAPP_FEES set AMOUNT = null;
    
    alter table EVAPP_FEES modify AMOUNT NUMBER(14,2);
    
    update EVAPP_FEES set AMOUNT = AMOUNT_TEMP;
    
    alter table EVAPP_FEES drop column AMOUNT_TEMP;
    
    0 讨论(0)
  • 2020-12-13 09:21

    By setting the scale, you decrease the precision. Try NUMBER(16,2).

    0 讨论(0)
提交回复
热议问题