tags:

views:

61

answers:

4

SELECT SCOPE_IDENTITY is always resulting id=1, what i have done wrong? how to get the currentid of the inserted element.

+1  A: 

Are you doing it within the same trasaction? Is the identity actually being incremented in the Database?

Climber104
yes,the identity is actually being incremented in the Database
user
A: 

Is the column set as Is Identity=Yes?

Keith
A: 

A good article on the different methods to retrieve the last inserted.

Spilarix
+1  A: 

SCOPE_IDENTITY does indeed return the identity column of the last inserted row in the scope you are in.

It can be messed up by a few things, within the same scope. In that case IDENT_CURRENT('myTable') will be better.

If not you'll want to post more code. SCOPE_IDENTITY does work as you think, so the problem is likely elsewhere.

Jon Hanna
How can `SCOPE_IDENTITY()` be messed up by triggers? I thought that was its advantage over `@@IDENTITY`.
Ian Henry
It shouldn't, but can in some cases. http://cgaskell.wordpress.com/2006/11/15/using-identity-vs-scope_identity-and-further-implications-with-triggers/
Jon Hanna
Have you verified this for yourself? The comment by angeliii seems to imply some user error on the part of the blog author.
Martin Smith
No, and it does seem suspect. One minute...
Jon Hanna
Right you are, that post is wrong. Editting post.
Jon Hanna