Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Getting the last entry in identity column?
Message
 
General information
Forum:
Microsoft SQL Server
Category:
Other
Environment versions
SQL Server:
SQL Server 2005
Miscellaneous
Thread ID:
01549403
Message ID:
01549405
Views:
26
>>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.
"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
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform