I am working on a content management system for updates where the administrator can add, edit and delete updates.
In the form where the admin adds an update, I want to create a textbox which displays the next value of an isIdentity column from the database called ‘Update_ID’. I can’t just retrieve the last row from the database and add 1 to it since if the admin deletes the last record in the database, the value in the textbox will not reflect the real ‘Update_ID’ of the next row.
How can this be solved please? Is there a way to get the actual value of the next isIdentity column in the database? Thanks 🙂
If you’re interested in doing something like this, the only effective way is going to be to actually insert the blank row and return that row to the user for editing, because you can’t know what the next ID is going to be without locking the table for inserts, which is probably not what you want to do.
A more effective solution might be to not fill out the Update_ID field when the user enters the data initially, but then inform them of the ID you just created afterwards by selecting SCOPE_IDENTITY or returning the value from the insert statement.
EDIT:
One possible alternative for what you want to do (if you choose to upgrade to SQL Server 2012) is to use the new Sequences feature. I’ve never used it before, though.