4

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

Binary Worrier
  • 50,774
  • 20
  • 136
  • 184

3 Answers3

9
SELECT IDENT_CURRENT('MyTable')

See BOL

AdaTheDev
  • 142,592
  • 28
  • 206
  • 200
5
SELECT IDENT_CURRENT('TABLE_NAME')

returns NULL if there is no identity defined in the table

il_guru
  • 8,383
  • 2
  • 42
  • 51
1
 select @@identity

might work,

there is also

SELECT SCOPE_IDENTITY()
Brandon Frohbieter
  • 17,563
  • 3
  • 40
  • 62
  • 2
    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 Oct 28 '10 at 13:11
  • 2
    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. ** – Adriaan Stander Oct 28 '10 at 13:12
  • 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 Oct 28 '10 at 13:54