0

I'm running into an issue when doing a row insert and trying to return the last primary key. I've attempted to do a $db->lastInsertId(); but get a driver issue which leads me to believe based off research that the driver I'm using for MSSQL doesn't actually support that function. So I'm looking at other options that would be safe for a high traffic website.

Is SELECT SCOPE_IDENTITY(); a good option for this or will that potentially caused issues with data being inserted if two rows are inserted at the same time?

Cody J. Mathis
  • 652
  • 1
  • 9
  • 25
  • if you execute multiple inserts in a single query, then you're hosed. you can only ever retrieve the ID generated by the **LAST** insert query executed. if you want the id for multiple inserts, then do `query($insert1); $id1 = ...; query($insert2); $id2 = ...`, etc... and retrieve the ID after each one. – Marc B May 19 '16 at 15:37
  • I just need the ID after the last insert. There is only ever one insert going on per statement but if two users try to submit at the very same time I'm worried that doing a SCOPE_IDENTITY(); will return the wrong row. I would imagine this being a valid concern but do I have any other option? – Cody J. Mathis May 19 '16 at 15:44
  • Last insert id is limited to the current db connection, if you arent doing anything very special you start a new connection on each request - meaning another user can not interfere – JimL May 19 '16 at 15:59
  • I suppose I was barking up the wrong tree. Thank you @MarcB for pointing me in the right direction. – Cody J. Mathis May 19 '16 at 16:19

0 Answers0