Using a trigger to simulate a second identity column in SQL Server 2005

后端 未结 5 1775
我寻月下人不归
我寻月下人不归 2020-12-20 07:46

I have various reasons for needing to implement, in addition to the identity column PK, a second, concurrency safe, auto-incrementing column in a SQL Server 2005 database.

5条回答
  •  一个人的身影
    2020-12-20 08:02

    I would disallow all the deletes from this table altogether. Instead of deleting, I would mark rows as available or inactive. Instead of inserting, I would first search if there are inactive rows, and reuse the one with the smallest ID if they exist. I would insert only if there are no available rows already in the table.

    Of course, I would serialize all inserts and deletes with sp_getapplock.

    You can use a trigger to disallow all deletes, it is simpler than filling gaps.

提交回复
热议问题