简体   繁体   English

仅在插入记录时才增加标识

[英]identity to be incremented only if record is inserted

sql server 2005:我在employee表中有一个empid列,带有标识on。如果在将数据插入表时出现了一些错误。同步性增加。我希望只有在插入记录时才增加标识。如果我已经生成了emp id从1到5然后在第6个记录插入错误ocurrs.and下一个记录插入标识值将是7.i希望它是6。

Why do you want to do that ? 你为什么要那样做?

The identity column should only be used as an 'internal administrative value' for the database, and it should have no 'business value', so why does it matter that there are gaps in that sequence ? 标识列应仅用作数据库的“内部管理值”,并且它应该没有“业务价值”,那么为什么该序列中存在空白呢?
If identity is used correctly, then users of your software will never be faced with the column that has an identity value; 如果正确使用了身份,那么您的软件用户将永远不会面对具有身份值的列; you just use it to uniquely identify a record. 你只需用它来唯一地识别一条记录。

I don't think this can be done. 我不认为这可以做到。 If you want your identity numbers to be exactly sequential then you may have to generate them yourself, rather than using the SQL Identity feature. 如果您希望您的身份编号完全按顺序排列,那么您可能必须自己生成它们,而不是使用SQL身份标识功能。

edit: Even rolling back the failed transactions will not make the Identity count go back down, this is by design, see this other question . 编辑:即使回滚失败的事务也不会使身份计数恢复,这是设计,请参阅另一个问题

What valid business reason do you have for caring if there are gaps? 如果存在差距,您有什么有效的商业理由来关心? There is no reason for the database to care and every reason to want to make sure that identity values are never reused for something else as they can cause major problems with data integrity with looking up information based on old reports, etc. Suppose you have a report that shows the orders last month and then you delete one of the records becasue the customer was duplicated and thus dedupped. 数据库没有理由关心并且每个理由都希望确保身份值永远不会被重用于其他东西,因为它们可能会导致数据完整性出现重大问题,并根据旧报告查找信息等。假设您有报告显示上个月的订单,然后您删除其中一个记录,因为客户是重复的,因此被重复删除。 Then you reuse the identity field for the dupped customer that was removed. 然后,您为已删除的重复客户重用身份字段。 NOw someone looknig at last month's report goes to look up customer 12345 and the data associated with that cuisotmer belongs to John Smith rather than Sally Jones. 上个月没有人看上去的报告去查找客户12345,与该菜肴相关的数据属于John Smith而不是Sally Jones。 BUt the person doesn;t know that because she is using an aggreagate, so now she has incorrect information that was totally avoidable. 因为她使用的是聚合物,所以不要知道这个人,所以现在她的信息是完全可以避免的。 If she was looking up the delted customer, the process instead could have redirected her to the correct customer left after the dedupping. 如果她正在查找已淘汰的客户,则该流程可能会将她重定向到重复扣除后留下的正确客户。

When you need to have this specific behaviour you should use stored procedures to generate the ID. 当您需要具有此特定行为时,应使用存储过程来生成ID。 This way you can really to a rollback. 这样你就可以真正地回滚了。 But keep in mind that the current behaviour is by purpose. 但请记住,目前的行为是出于目的。 Transaction isolation and different read levels (dirty reads) will most likely get you into trouble when you don't use locking on that id field in your masterdata table that holds the current or next ID value. 当您不在masterdata表中使用包含当前或下一个ID值的id字段的锁定时,事务隔离和不同的读取级别(脏读)很可能会让您遇到麻烦。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM