Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Oracle: How do I get the sequence number of the row just inserted?

Tags:

How do I get the sequence number of the row just inserted?

like image 664
Mark Harrison Avatar asked Dec 11 '08 22:12

Mark Harrison


People also ask

How do I get the last sequence number in SQL?

you have to intiate the sequence for your session using sequence. nextval and sequence. currval will show the result.

How do you get the next value of a sequence in PL SQL?

After a sequence is created, you can access its values in SQL statements with the CURRVAL pseudocolumn, which returns the current value of the sequence, or the NEXTVAL pseudocolumn, which increments the sequence and returns the new value.

What is Oracle sequence number?

A sequence is an object in Oracle that is used to generate a number sequence. This can be useful when you need to create a unique number to act as a primary key.

What is Oracle sequence Nextval?

The Oracle NEXTVAL function is used to retrieve the next value in a sequence. The Oracle NEXTVAL function must be called before calling the CURRVAL function, or an error will be thrown. No current value exists for the sequence until the Oracle NEXVAL function has been called at least once.


1 Answers

insert ... returning.

declare    s2 number;  begin    insert into seqtest2(x) values ('aaa') returning seq into s2;    dbms_output.put_line(s2);  end; 

"seq" here refers to the name of the column whose value you want to store into variable s2.

in python:

myseq=curs.var(cx_Oracle.NUMBER) curs.prepare("insert into seqtest2(x) values('bbb') returning seq into :x") curs.execute(None, x=myseq) print int(myseq.getvalue()) 
like image 172
Mark Harrison Avatar answered Oct 21 '22 15:10

Mark Harrison