Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return a value from a insert statement

Working with an Oracle 9i database from an ASP.NET 2.0 (VB) application using OLEDB. Is there a way to have an insert statement return a value? I have a sequence set up to number entries as they go into the database, but I need that value to come back after the insert so I can do some manipulation to the set I just entered in the code-behind VB.

like image 815
Maximillian Avatar asked Nov 06 '08 23:11

Maximillian


2 Answers

Some possibilities:

1) Use the RETURNING clause:

INSERT INTO emp (empno, ename) VALUES (emp_seq.NEXTVAL, 'ANDREWS')
RETURNING empno INTO :variable;

2) Use the sequence CURRVAL:

INSERT INTO emp (empno, ename) VALUES (emp_seq.NEXTVAL, 'ANDREWS');
SELECT emp_seq.CURRVAL INTO :variable FROM DUAL;

CURRVAL returns the last sequence value generated by your session.

like image 115
Tony Andrews Avatar answered Sep 28 '22 07:09

Tony Andrews


Oracle seem to have a keywod called "returning" which can return a given column of the inserted row, however that might require you to set the "autoincrement" field manually by invoking the next value in your sequence.

Check this discussion about it:

http://forums.oracle.com/forums/thread.jspa?threadID=354998

However, you can always select the current sequence-number in a second query, sort of like MySQLs last_insert_id()

like image 35
jishi Avatar answered Sep 28 '22 07:09

jishi