Click here to Skip to main content
16,007,277 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
which is better?

Select @@identity or select MAX().
Posted
Comments
BulletVictim 9-Apr-15 1:57am    
It depends on where you need to get the top Identity column value. In SQL it is usually more accurate to use @@IDENTITY when you require the Identity column value of a newly inserted value in the table to be used later on in the same stored procedure.
Using max(identity column) usually works better when you require the value of pre-existing records in the table before an insert can happen.
Both works the same at the end of the day, it all comes down your preference

@@identity will avoid the need for a table scan.
MAX would scan the table.

There can be many more ways to pick the identity - 6 Different Ways To Get The Current Identity Value in SQL[^].
 
Share this answer
 
aciobanita constantin wrote:
which is better?

Select @@identity or select MAX()


Hhmmm... This question is not well formulated. The context of usage of @@identity or MAX() depends on many factors.

If you would like to find max value in table, use MAX function, but... If you would like to newly added record, use @@identity.

For further information, please see:
MS Access - MAX() function[^]
Retrieving Identity or Autonumber Values[^] + How to retrieve last autoincremented value in MS-Access like @@Identity in Sql Server[^]

You may want to get latest value using MAX function, but i need to warn you: in multi-user environment, MAX function can return wrong value. Imagine, a time needed to add new record and to return MAX value is counted in miliseconds. In MS Access database changes are made locally, so...

Well, as you can see, none of above is "better" or "worse".
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900