Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Get the last identity value for a table?
Message
 
General information
Forum:
Microsoft SQL Server
Category:
Other
Environment versions
SQL Server:
SQL Server 2005
Miscellaneous
Thread ID:
01421835
Message ID:
01421856
Views:
27
>>>>>>It's not the last value in the IDENTITY column but the last IDENTITY value generated for a specified table. To get the last value in the column you'll have to run a query with MAX() on that column.
>>>>>
>>>>>I get it. I only use IDENTITY for a PK column so in my case I have only one IDENTITY per table. In this case doing MAX() on the column and IDENT_CURRENT would give the same result, right?
>>>>
>>>>It may not. The next IDENTITY value is generated outside of any transaction. If the transaction that causes next value to be generated is rolled back than that value will not make it in the table.
>>>
>>>I see. Then, if I use MAX() and the table is large (say 100,000 rows), will SQL take a second (or maybe several) to calculate the max value of the column? (btw, the syntax is "MAX('MyColumnName')", right?)
>>
>>
>>It may take several seconds.
>>BTW, you can have only one IDENTITY column per table.
>
>Ok. I believe in my case the chance of another transaction being rolled back while I do this transaction is very small (I think I will sooner win a MegaMillions lottery <g>) so I will use IDENT_CURRENT. I am guessing that it is much faster than MAX. Also, thanks for clarifying that there can be only one IDENTITY per column.
>
>I only wish that I could get the value of IDENT_CURRENT using VFP SPT without getting a cursor and then having to get the value of a column in this cursor. Worlds is not perfect <g>.
>
>Thank you very much for your help!

Dmitry,

Why you don't want to use SCOPE_IDENTITY() or new Output clause in INSERT command? What is the ultimate goal?
If it's not broken, fix it until it is.


My Blog
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform