I’m working in Visual Studio – VB.NET.
My problem is that I want to delete a specific row in SQL Server but the only unique column I have is an Identity that increments automatically.
My process of work:
1. I add a row in the column (the identity is being incremented, but I don’t know the number)
2. I want to delete the previous row
Is there a sort of unique ID that every new record has?
It’s possible that my table has 2 exactly the same records, just the sequence (identity) is different.
Any ideas how to handle this problem?
SQL Server has a few functions that return the generated ID for the last rows, each with it’s own specific strengths and weaknesses.
Basically:
@@IDENTITYworks if you do not use triggersSCOPE_IDENTITY()works for the code you explicitly called.IDENT_CURRENT(‘tablename’)works for a specific table, across all scopes.In almost all scenarios
SCOPE_IDENTITY()is what you need, and it’s a good habit to use it, opposed to the other options.A good discussion on the pros and cons of the approaches is also available here.