How to create id with AUTO_INCREMENT on Oracle?

后端 未结 16 1666
死守一世寂寞
死守一世寂寞 2020-11-21 04:52

It appears that there is no concept of AUTO_INCREMENT in Oracle, up until and including version 11g.

How can I create a column that behaves like auto increment in Or

16条回答
  •  面向向阳花
    2020-11-21 05:59

    FUNCTION GETUNIQUEID_2 RETURN VARCHAR2
    AS
    v_curr_id NUMBER;
    v_inc NUMBER;
    v_next_val NUMBER;
    pragma autonomous_transaction;
    begin 
    CREATE SEQUENCE sequnce
    START WITH YYMMDD0000000001
    INCREMENT BY 1
    NOCACHE
    select sequence.nextval into v_curr_id from dual;
    if(substr(v_curr_id,0,6)= to_char(sysdate,'yymmdd')) then
    v_next_val := to_number(to_char(SYSDATE+1, 'yymmdd') || '0000000000');
    v_inc := v_next_val - v_curr_id;
    execute immediate ' alter sequence sequence increment by ' || v_inc ;
    select sequence.nextval into v_curr_id from dual;
    execute immediate ' alter sequence sequence increment by 1';
    else
    dbms_output.put_line('exception : file not found');
    end if;
    RETURN 'ID'||v_curr_id;
    END;
    

提交回复
热议问题