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

后端 未结 8 1342
[愿得一人]
[愿得一人] 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:45

    Here I have my example which works just fine:

    declare
     ex number;
    begin
      select MAX(MAX_FK_ID)  + 1 into ex from TABLE;
      If ex > 0 then
        begin
                execute immediate 'DROP SEQUENCE SQ_NAME';
          exception when others then
            null;
        end;
        execute immediate 'CREATE SEQUENCE SQ_NAME INCREMENT BY 1 START WITH ' || ex || ' NOCYCLE CACHE 20 NOORDER';
      end if;
    end;
    

提交回复
热议问题