Ali Saad
asked on
Identity Column in SQL Server
Hello
I have a table with an Identity Column
Create Table T1( Col1 INT Identity Primary Key ,COL2nvarchar(100) ...........)
When the insert statement failed for any reason the Col1 still increasing .....
1- Can i change this behaviour ? I means is there a documented way to prevent the Auto increment IF the insert operation failed ?
2- When i use Scope_IDENTITY() function it Returns the SUCCSSED inserted Identity .. what about if INSERT failed how can i get the autoIncremental number for the failed insert?
I have a table with an Identity Column
Create Table T1( Col1 INT Identity Primary Key ,COL2nvarchar(100) ...........)
When the insert statement failed for any reason the Col1 still increasing .....
1- Can i change this behaviour ? I means is there a documented way to prevent the Auto increment IF the insert operation failed ?
2- When i use Scope_IDENTITY() function it Returns the SUCCSSED inserted Identity .. what about if INSERT failed how can i get the autoIncremental number for the failed insert?
Sorry, I have to correct myself. Transaction does not affect Identity value and you cannot use it to keep the identity column values consecutive.
So, the only way it to use your own procedure which will generate "identity" values based on some table containing counters. You'll also need a mechanism to reuse the generated counter value when the insert command fails.
So, the only way it to use your own procedure which will generate "identity" values based on some table containing counters. You'll also need a mechanism to reuse the generated counter value when the insert command fails.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
2) You may select MAX value from the table.