Identity counter increment by one although it is in TRY Catch and Transaction is roll-backed ? SSMS 2008 is there any way i can stop it +1 or rollback it too.
相关问题
- sql execution latency when assign to a variable
- What is the best way to cache a table from a (SQL)
- php PDO::FETCH_ASSOC doesnt detect select after ba
- Bulk update SQL Server C#
- SQL to Parse a Key-Value String
相关文章
- Entity Framework 4.3.1 failing to create (/open) a
- Code for inserting data into SQL Server database u
- Delete Every Alternate Row in SQL
- Linux based PHP install connecting to MsSQL Server
- SQL Azure Reset autoincrement
- How do we alias a Sql Server instance name used in
- Is recursion good in SQL Server?
- How can I convert a OLE Automation Date value to a
In order to understand why this happened, Let's execute below sample code first-
Now, Execution plan for above query is-
The second last operator from right
Compute Scalar
is computing value for[Expr1003]=getidentity((629577281),(2),NULL)
which is IDENTITY value for ID column. So this clearly indicates that IDENTITY values are fetched & Incremented prior to Insertion (INSERT Operator
). So its by nature that even transaction rollback at later stage once created IDENTITY value is there.Now, in order to reseed the IDENTITY value to Maximum Identity Value present in table + 1, you need sysadmin permission to execute below DBCC command -
So the final query should include below piece of code prior to rollback statement:-
So it is recommended to leave it on SQL Server.
You are right and the following code inserts record with
[Col01]
equal to2
:This is by design (as you can see in the documentation:
I try using
NOCACHE
sequence but it does not work onSQL Server 2012
:You can use
MAX
to solve this:But you must pay attentions to your isolation level for potential issues:
If you want to insert many rows at the same time, do the following: