Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sequence creation in oracle

I want to create a sequence in oracle where the max value of the column field (Empid) must be the min value of the sequence.

The below was the one i found in our same stackexchange

create sequence mytemp_seq start with &v_Startval;

This command prompts me to enter the max value of teh column name which i have to enter.

How can I fix the value of &v_startval with out it prompting ,but directly setting the values from the below statement

select max(empid) from mytemp..

I am trying like this below

create sequence mytemp_seq start with (SELECT MAX(empid) from mytemp)

But it doesnt work.

like image 246
user682571 Avatar asked May 19 '11 10:05

user682571


People also ask

What is create sequence in Oracle?

Use the CREATE SEQUENCE statement to create a sequence, which is a database object from which multiple users may generate unique integers. You can use sequences to automatically generate primary key values.

What is sequence in Oracle with example?

An Oracle Sequence is a database object, just like a table or view, that represents a sequence of integers that can be used by any table or view in the global database namespace. A Sequence's values can be accessed using the NEXTVAL, and CURRVAL pseudo-columns. A Sequence can be ascending or descending.

How do you create a sequence in 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.


2 Answers

You could do it with some PL/SQL:

declare
  v_startval integer;
begin
  select max(empid)+1 into v_startval from mytemp;
  execute immediate 'create sequence mytemp_seq start with ' || v_startval;
end;
like image 128
Tony Andrews Avatar answered Nov 03 '22 00:11

Tony Andrews


In sqlplus you can do

col max_id new_value seq_min_val  
SELECT MAX(empid)+1 AS max_id from mytemp;
create sequence mytemp_seq start with &seq_min_val;
like image 28
schurik Avatar answered Nov 03 '22 00:11

schurik