Issue while inserting in a table using sequence in Oracle 11g Rel 2
894242Oct 13 2011 — edited Oct 14 2011Hi All,
I am facing issue when I am inserting sequence values in my table.
While inserting, my sequence does not start with the start with value.
Example Script
CREATE SEQUENCE xyz_seq
START WITH 1
INCREMENT BY 1
NOMAXVALUE
MINVALUE 1
NOCYCLE
NOCACHE
NOORDER;
create table abc (an number not null);
insert into abc values (xyz_seq.nextval );
select *
from abc;
drop sequence xyz_seq;
drop table abc;
Output
Sequence created.
Table created.
1 row created.
AN
----------
2
1 row selected.
Sequence dropped.
Table dropped.
I am unable to understand why it is inserting value 2, when my sequence should start at 1.
To overcome this road block, I implemented a different logic.
Example
CREATE SEQUENCE xyz_seq
START WITH 1
INCREMENT BY 1
NOMAXVALUE
MINVALUE 1
NOCYCLE
NOCACHE
NOORDER;
create table abc (an number not null);
declare
x number(1) := xyz_seq.nextval;
begin
insert into abc values (x );
end;
select *
from abc;
drop sequence xyz_seq;
drop table abc;
Output
Sequence created.
Table created.
PL/SQL procedure successfully completed.
AN
----------
1
1 row selected.
Sequence dropped.
Table dropped.
However, my question still remains that why referring to the sequence.nextval in my insert does not return me the start with value.
Regards,
Rishi