Am I correct in understanding that mysql's LAST_INSERT_ID() function doesn't reset between insert attempts (as @@identity does in sql-server)... that if the immediately preceding insert operation fails, LAST_INSERT_ID() will return the pk of whatever the pk of that connection's last insert to any table with an auto-incrementing primary key was. And if I am correct in this, does this not seem like just about the most retarded behaviour for this function that one could come up with? Is there no mysql function that behaves similarly to sql-server's @@identity? One that will return NULL if the immediately preceding insert attempt creates no new record? Alternatively, how does one know with certainty the primary key of the most recent insert operation?
@@identity is usually wrong in sql server as well. You should be using scope_identity()
in most cases instead.
In MySql, last_insert_id() should be safe to use because the only way you can call this function after an insert error is you've already correctly caught and accounted for the insert error. Otherwise you would still be processing commands. In other words, last_insert_id() is not your error-handling mechanism.