oracle-databasesequencedefault-valueoracle19cora-00904

ORA-02262: ORA-904 occurs while type-checking column default value expression while modify by sequence defaults


Why I got error? Column datatype is integer like a sequence value...

SQL> create sequence  SEQ_TEST_ID;

Sequence created.

SQL> desc test
 Name                                                              Null?    Type
 ----------------------------------------------------------------- -------- --------------------------------------------
 VAL                                                                        NUMBER(38)
 D                                                                          DATE
 T                                                                          VARCHAR2(4000)
 N                                                                          NUMBER(8,2)
 S                                                                          TIMESTAMP(6)
 TEST_ID                                                                    NUMBER(38)
 PRIM_ID                                                                    NUMBER(38)

SQL> ALTER TABLE TEST MODIFY (TEST_ID DEFAULT SEQ_TEST_ID);
ALTER TABLE TEST MODIFY (TEST_ID DEFAULT SEQ_TEST_ID)
                         *
ERROR at line 1:
ORA-02262: ORA-904 occurs while type-checking column default value expression

alter table got error


Solution

  • You need to modify the column not the sequence, but he nexe value SEQ_TEST_ID.nextval

    It would create an new value every time you inser a row.

    if your table has already has row, you first need to update the column TEST_ID

    create sequence  SEQ_TEST_ID;
    
    CREATE tABLE TEST (TEST_ID NUMBER(30))
    
    ALTER TABLE TEST
        MODIFY TEST_ID INT DEFAULT SEQ_TEST_ID.nextval;
    

    fiddle