将数据插入到SQL Server数据表中 [英] Insert data in to sql server data table

查看:164
本文介绍了将数据插入到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数据表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆