Create a Sequence with START WITH from Query

后端 未结 2 997
失恋的感觉
失恋的感觉 2020-12-10 12:35

How can I create a Sequence where my START WITH value comes from a query?

I\'m trying this way: CREATE SEQUENCE \"Seq\" INCREMENT BY 1 START WITH (SELECT MAX(

2条回答
  •  伪装坚强ぢ
    2020-12-10 12:59

    The START WITH CLAUSE accepts an integer. You can form the "Create sequence " statement dynamically and then execute it using execute immediate to achieve this.

    declare
        l_new_seq INTEGER;
    begin
       select max(id) + 1
       into   l_new_seq
       from   test_table;
    
        execute immediate 'Create sequence test_seq_2
                           start with ' || l_new_seq ||
                           ' increment by 1';
    end;
    /
    

    Check out these links.

    http://download.oracle.com/docs/cd/B14117_01/server.101/b10759/statements_6014.htm
    http://download.oracle.com/docs/cd/B19306_01/appdev.102/b14261/executeimmediate_statement.htm

提交回复
热议问题