将数据插入到SQL Server数据表中

编程入门 行业动态 更新时间:2024-10-27 16:28:06
本文介绍了将数据插入到SQL Server数据表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个名为customer_master的表,其中包含一个名为cust_id的列,其自动增量设置为1.

I have one table called customer_master that includes a column called cust_id with autoincrement set to 1.

当我们尝试插入记录时,其工作正常,并且插入了诸如cust_id,1、2、3和4之类的插入记录,但是当insert命令中生成错误时,我们进行事务回滚,这意味着 5不会插入,但是当我们插入另一个记录时,cust_id会生成6.它会跳过cust_id5.

When we try to insert records its working fine and inserted records like cust_id 1, 2, 3 and 4 are inserted, but when an error is generated in the insert command we do a transaction rollback, this means that cust_id 5 is not inserted, but when we are insert another record, cust_id generates 6. It skips cust_id 5.

我想对其进行设置,以便在插入命令中生成任何错误时,标识都不会递增.

I want to set it up so that if any error is generated in the insert command the identity is not incremented.

我们正在使用c#和sql server 2005.

We are using c# and sql server 2005.

推荐答案

SQL Server这样做的原因是为了提高效率.如果您需要没有间隙的序列号,则不应该使用identity,则需要实现自己的方案,在该方案中,阻止并发事务等待下一个值,以防初始事务回滚.

The reason SQL Server does this is for efficiency. If you need a sequence number without gaps you shouldn't be using identity you would need to implement your own scheme where concurrent transactions are blocked waiting for the next value just in case the initial transaction rolls back.

此处的第二个查询可用于目的.但是您真的需要这个吗?如果仅出于美学目的,我的建议就是不要担心!

The second query here could be used for that purpose. But do you really need this? If it is purely for aesthetic purposes my advice is not to worry about it!

更多推荐

将数据插入到SQL Server数据表中

本文发布于:2023-10-12 17:39:21,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1485411.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据表   数据   SQL   Server

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!