views:

23

answers:

3

dbcc checkident (MyTable, NORESEED)

Will show the identity value in a message. I can't see the message as I need to select the identity value through a DataReader.

Anyone any ideas?

Thanks folks

+5  A: 
SELECT IDENT_CURRENT('MyTable')

See BOL

AdaTheDev
Excellent, I knew there had to be a way to do it. Thanks :)
Binary Worrier
A: 
 select @@identity

might work,

there is also

SELECT SCOPE_IDENTITY()
Orbit
why -1? ....... perfectly valid
Orbit
I'm guess the downvote, because that returns the identity of the row you just inserted. I'm not inserting any rows, but need to know the current identity value of the table
Binary Worrier
This is not correct. @@identity can be for any table. See http://msdn.microsoft.com/en-us/library/aa933167(SQL.80).aspx. **@@IDENTITY and SCOPE_IDENTITY will return the last identity value generated in any table in the current session. **
astander
although IDENT_CURRENT seems to be the correct answer here.
Orbit
You don't specify the table to get the last identity from. So you can't get the identity of any table. It's always the table where you have inserted some rows just before.
Stefan Steinegger
+4  A: 
SELECT IDENT_CURRENT('TABLE_NAME')

returns NULL if there is no identity defined in the table

il_guru