How to create an Oracle sequence starting with max value from a table?

后端 未结 8 1339
[愿得一人]
[愿得一人] 2020-12-02 20:14

Trying to create a sequence in Oracle that starts with the max value from a specific table. Why does this not work?

CREATE SEQUENCE transaction_sequence
  MI         


        
8条回答
  •  悲&欢浪女
    2020-12-02 20:37

    Based on Ivan Laharnar with less code and simplier:

    declare
        lastSeq number;
    begin
        SELECT MAX(ID) + 1 INTO lastSeq FROM ;
        if lastSeq IS NULL then lastSeq := 1; end if;
        execute immediate 'CREATE SEQUENCE  INCREMENT BY 1 START WITH ' || lastSeq || ' MAXVALUE 999999999 MINVALUE 1 NOCACHE';
    end;
    

提交回复
热议问题