Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a Sequence with START WITH from Query

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("ID") FROM "Table");

But, I get the ORA-01722 error

like image 366
Victor Avatar asked Aug 11 '10 17:08

Victor


People also ask

How do you create a query sequence?

Following is the sequence query creating sequence in ascending order. Example 1: CREATE SEQUENCE sequence_1 start with 1 increment by 1 minvalue 0 maxvalue 100 cycle; Above query will create a sequence named sequence_1.

How do I create a sequence number in SQL query?

The syntax to create a sequence in SQL Server (Transact-SQL) is: CREATE SEQUENCE [schema.] sequence_name [ AS datatype ] [ START WITH value ] [ INCREMENT BY value ] [ MINVALUE value | NO MINVALUE ] [ MAXVALUE value | NO MAXVALUE ] [ CYCLE | NO CYCLE ] [ CACHE value | NO CACHE ]; AS datatype.

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

The syntax to create a sequence in Oracle is: CREATE SEQUENCE sequence_name MINVALUE value MAXVALUE value START WITH value INCREMENT BY value CACHE value; sequence_name. The name of the sequence that you wish to create.

How do I start a sequence in Oracle?

To create a sequence in another user's schema, you must have the CREATE ANY SEQUENCE system privilege. Specify the schema to contain the sequence. If you omit schema , then Oracle Database creates the sequence in your own schema. Specify the name of the sequence to be created.


1 Answers

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

like image 113
Rajesh Chamarthi Avatar answered Sep 25 '22 03:09

Rajesh Chamarthi