Should use SCOPE_IDENTITY() rather than @@IDENTITY #18

Closed
adutton opened this Issue Mar 9, 2011 · 3 comments

4 participants

@adutton

You should change the SQL in Insert(object o) from:
cmd.CommandText = "SELECT @@IDENTITY as newID";
to
cmd.CommandText = "SELECT SCOPE_IDENTITY() as newID";

With the current code, you may get the wrong ID if users have triggers (that perform inserts) on their tables. Using SCOPE_IDENTITY() ensures you get the right ID back, no matter what.

@kamranayub

I've added this to my fork and pull request: #17

@datachomp

Adutton, Scope_identity() isn't supported in SQL Server CE which is some peoples primary use case for Massive.

In my version, I completely remove that section and tagteam the scope_identity() to the actual insert command. I'm far too snobby to use SQL Server CE so I don't fear such a change. Anyhow, it's a light/easy codebase and easy to hop in and get it work how you want it.

@robconery
Collaborator

SCOPE_IDENTITY() was removed from the original version for the reasons DataChomp mentions (primary focus is SQL CE). Simple to replace the call.

@robconery robconery closed this Apr 29, 2011
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment