Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

switched from @@identity to scope_identity() for inserts, because of triggers #185

wants to merge 2 commits into


None yet
2 participants

zvolkov commented Jan 24, 2013

If a table being inserted into has an AFTER INSERT trigger that itself does some inserts, the @@identity variable will return the ID inserted by the trigger. The newer scope_identity() function limits the scope of IDs to user's code only.

@zvolkov zvolkov commented on the diff Jan 24, 2013

@@ -502,9 +502,8 @@ private dynamic BuildPagedResult(string sql = "", string primaryKeyField = "", s
if (BeforeSave(ex)) {
using (dynamic conn = OpenConnection()) {
var cmd = CreateInsertCommand(ex);
+ cmd.CommandText = cmd.CommandText + ";SELECT SCOPE_IDENTITY() as newID";

zvolkov Jan 24, 2013


SCOPE_IDENTITY must be executed as part of the same batch, otherwise it won't see the ID -- that's why I'm appending it to the same command as opposed to running separately


robconery commented Feb 19, 2013

The original use case for Massive was WebMatrix - so we rolled with @@identity as SQL Compact doesn't have SCOPE_IDENTITY(). I felt at the time (and still do) that if you'd like/need to use SCOPE_IDENTITY you can - just change your code.

Also - "performance improvements" ... not sure what you're doing there and it might be nice to have a use case and resolution.

@robconery robconery closed this Feb 19, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment