>>>>Hi,
>>>>
>>>>Do you think the following two approaches of getting the last entry in the identity column always give the same result?
>>>>
>>>>
>>>>select IDENT_CURRENT( 'table_name' )
>>>>
>>>>
>>>>
>>>>select top 1 IdentColumn from table_name group by IdentColumn order by IdentColumn desc
>>>>
>>>>
>>>>And if yes, am I correct in assuming that the first method (using IDENT_CURRENT()) would be a faster way of getting the value?
>>>>
>>>>TIA.
>>>
>>>The correct way is to use OUTPUT clause of the INSERT command. The second best method is to use SCOPE_IDENTITY() function. All other methods should not be used.
>>>
>>>See this blog post
>>>
http://blog.sqlauthority.com/2007/03/25/sql-server-identity-vs-scope_identity-vs-ident_current-retrieve-last-inserted-identity-of-record/>>
>>I was not talking of getting the value during the insert. My question was if at any point your application needs to know the last value (that could have been inserted 2 weeks ago), which method would be correct and faster.
>
>I see. I think the second method (without GROUP BY, of course) is a correct method. The first one may return wrong value if that row was deleted.
I see. So the IDENT_CURRENT() does not change if the last row is deleted. I didn't know that. Thank you.
"The creative process is nothing but a series of crises." Isaac Bashevis Singer
"My experience is that as soon as people are old enough to know better, they don't know anything at all." Oscar Wilde
"If a nation values anything more than freedom, it will lose its freedom; and the irony of it is that if it is comfort or money that it values more, it will lose that too." W.Somerset Maugham