Santhosh
Santhosh

Reputation: 8207

Auto increment a field without sequence in ORACLE

I am trying to increment a field without sequence . Is there any possiblities?? can i do something like this

INSERT INTO Test 
VALUES ((
         SELECT COUNT(ID) 
         FROM Test)+1)

Please suggest me a way to do this

Upvotes: 0

Views: 7139

Answers (2)

user2038893
user2038893

Reputation: 344

If your sequence field is called seq_fld for example you could use

insert into Test values ( (select max(seq_fld) from Test) + 1)

It is advisable to have unique constraint on seq_fld

Upvotes: 1

RobertKing
RobertKing

Reputation: 1921

you can take the current maximum value in a variable with @var_name = SELECT MAX(column_name) FROM TABLE

then you can make use of ROW_NUMBER() OVER (ORDER BY column_name)+@var_name

Upvotes: 1

Related Questions