即使事务回滚,SQL 标识(自动编号)也会增加

我有一个。带有 SQLServer2005数据库的 SQL 插入的 net 事务。该表具有标识主键。

当事务中发生错误时,将调用 Rollback()。行插入被正确地回滚,但是下次向表插入数据时,标识被递增,就好像回滚从未发生过一样。所以基本上在同一序列中存在间隙。有没有什么办法可以让 Rollback()方法恢复丢失的身份?

我这么做不对吗?

36233 次浏览

As far as I know the rows for insertion claim the autonumber and on rollback that number is lost for good. If you're depending on that autonumber being in sequencing you might want to consider the approach you're using.

I don't think there is any requirement that autonumbered keys be sequential. In fact, I don't think they can be required to be:

  • transaction a starts and inserts

  • transaction b starts and inserts

  • transaction a aborts

    you get a hole. nothing to do about it.

Muhan try to think of it in the context of many simultaneous connections executing this transaction and not one at a time. Some will fail and some will succeed. You want SQL Server to concentrate on running the new requests as they come in and not on maintaining a gap-less identity column. IMO it (gaps in the values) is definitely something not worth spending time on.

If you think about it, the auto-increment number should not be transactional. If other transactions had to wait to see if the auto-number was going to be used or "rolled back", they would be blocked by the existing transaction using the auto-number. For example, consider my psuedo code below with table A using an auto-number field for the ID column:

User 1
------------
begin transaction
insert into A ...
insert into B ...
update C ...
insert into D ...
commit




User 2
-----------
begin transaction
insert into A ...
insert into B ...
commit

If user 2's transaction starts a millisecond after user 1's, then their insert into table A would have to wait for user 1's entire transaction to complete just to see if the auto-number from the first insert into A was used.

This is a feature, not a bug. I would recommend using another scheme to generate auto-numbers if you need them to be tightly sequential.

No. Sequence implmentations use an autonomous transaction. In Oracle, the autonomous transaction was once internal to the dbms, but is now exposed for your own use (and is often used incorrectly)

PRAGMA AUTONOMOUS_TRANSACTION;'

You get gaps in your sequence if you DELETE a row too.

Sequences are required to be unique, but they are not required to be sequential. The fact that they are monotonically increasing is just a fluke of implementation.

If you depend on your identity values being gapless, then yes - you are doing it wrong. The whole point of a surrogate key to is to have no business meaning.

And, no, there is no way to change this behaivor (short of rolling your own autoincrement, and suffering the performance consequences of blocking other inserts).

All the other posters who say not to worry about it, and that you should get gaps, are right. If there's business meaning to the number, and that meaning doesn't jive with gaps, then don't use an identity column.

FYI, if for whatever reason you DO want to remove the gaps, most databases have a way to reseed the auto-numbering to the number of your choice. It's a pain in the arse, and if you find yourself needing to do it regularly, you definitely shouldn't be using an autonumber / identity field, as noted above. But here's the code to do it in SQL server:

DBCC CHECKIDENT('Product', RESEED, 0)

That sets the product table to start back at 1 (though if you have records in the table, it'll obviously skip the ID values that are already taken.) Other RDBMS vendors have their own syntax, but the effect is roughly the same, so look up "reseed identity" or "reseed autonumber" in the system help files or internets.

Again: this is for special occasions, not regular use. Don't put it in a stored procedure and make us all come over there.