How do I get the sequence number of the row just inserted?
you have to intiate the sequence for your session using sequence. nextval and sequence. currval will show the result.
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.
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.
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.
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())
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With