Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

identity

 
jesse harris
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
anyone know a good way to return the identity of a row that was just inserted?
 
Daniel Dunleavy
Ranch Hand
Posts: 276
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if you are talking about MS SQL SERVER....
you should call a stored procedure which does the insert and the last statement in the procedure is
select @@IDENTITY
Dan
 
Jamie Robertson
Ranch Hand
Posts: 1879
MySQL Database Suse
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by jesse harris:
anyone know a good way to return the identity of a row that was just inserted?

Another idea that works with oracle(it may be possible in SQLServer too) is to use a select statement to obtain the new identifier. In Oracle the database will give out the next available id (as if it were in an insert statement) then use the id in your insert statement.
Oracle code:
resultset = statement.executeQuery("select sequence.nextVal from dual");
long nextId = resultset.getLong(1);
--> this selects the next
id value from the database. dual is just a dummy table so you can accomplish the select statement.
statement.executeUpdate("insert into table (id, name) values (" + nextId + ", 'Joey Joe Joe Shabadoo')");
Jamie
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic