Sql Server 2005 身份列上的主键冲突
我遇到了一个奇怪的问题,我需要一些帮助来解决它。
我有一个数据库,除了所有应用程序数据列之外,还有一个 ID 列(定义为 int not null,Identity,从 1 开始,以 1 递增)。 表的主键是 ID 列,没有其他组件。
没有一组数据可以用作“自然主键”,因为应用程序必须允许多次提交相同的数据。
我有一个存储过程,这是向表中添加新记录的唯一方法(除了作为数据库所有者直接登录服务器之外)
当 QA 今天早上测试应用程序时,他们将新记录输入数据库(按预期使用应用程序以及过去两周的做法)并在此表上遇到主键违规。
这与我使用主键的方式已经有大约 10 年了,但从未遇到过这种情况。
有想法该怎么解决这个吗? 或者这是很长一段时间才会出现的宇宙射线故障之一。
感谢您提供的任何建议。
Nigel
在美国东部时间 6 月 12 日下午 1:15 编辑,以提供更多信息
架构的简化版本...
CREATE TABLE [dbo].[tbl_Queries](
[QueryID] [int] IDENTITY(1,1) NOT NULL,
[FirstName] [varchar](50) NOT NULL,
[LastName] [varchar](50) NOT NULL,
[Address] [varchar](150) NOT NULL,
[Apt#] [varchar](10) NOT NULL
... <12 other columns deleted for brevity>
[VersionCode] [timestamp] NOT NULL,
CONSTRAINT [PK_tbl_Queries] PRIMARY KEY CLUSTERED
(
[QueryID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
(还删除了默认值语句)
存储过程如下
insert into dbo.tbl_Queries
( FirstName,
LastName,
[Address],
[Apt#]...) values
( @firstName,
@lastName,
@address,
isnull(@apt, ''), ... )
它甚至不查看标识列,不'不要使用 IDENTITY、@@scope_identity 或类似的东西,它只是一个文件,然后就可以忘记了。
我确信身份值没有被重置,并且没有其他人使用直接数据库访问来输入值。 在此项目中,唯一一次使用身份插入是在初始数据库部署中,以在查找表中设置特定值。
QA 团队在收到错误后立即再次尝试,并成功提交了查询,此后他们一直在尝试重现该错误,但到目前为止尚未成功。
我真的很欣赏大家的想法。
I’m running into an odd problem, and I need some help trying to figure it out.
I have a database which has an ID column (defined as int not null, Identity, starts at 1, increments by 1) in addition to all the application data columns. The primary key for the table is the ID column, no other components.
There is no set of data I can use as a "natural primary key" since the application has to allow for multiple submissions of the same data.
I have a stored procedure, which is the only way to add new records into the table (other than logging into the server directly as the db owner)
While QA was testing the application this morning, they to enter a new record into the database (using the application as it was intended, and as they have been doing for the last two weeks) and encountered a primary key violation on this table.
This is the same way I've been doing Primary Keys for about 10 years now, and have never run across this.
Any ideas on how to fix this? Or is this one of those cosmic ray glitches that shows up once in a long while.
Thanks for any advice you can give.
Nigel
Edited at 1:15PM EDT June 12th, to give more information
A simplified version of the schema...
CREATE TABLE [dbo].[tbl_Queries](
[QueryID] [int] IDENTITY(1,1) NOT NULL,
[FirstName] [varchar](50) NOT NULL,
[LastName] [varchar](50) NOT NULL,
[Address] [varchar](150) NOT NULL,
[Apt#] [varchar](10) NOT NULL
... <12 other columns deleted for brevity>
[VersionCode] [timestamp] NOT NULL,
CONSTRAINT [PK_tbl_Queries] PRIMARY KEY CLUSTERED
(
[QueryID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
(also removed the default value statements)
The stored procedure is as follows
insert into dbo.tbl_Queries
( FirstName,
LastName,
[Address],
[Apt#]...) values
( @firstName,
@lastName,
@address,
isnull(@apt, ''), ... )
It doesn't even look at the identity column, doesn't use IDENTITY, @@scope_identity or anything similar, it's just a file and forget.
I am as confident as I can be that the identity value wasn't reset, and that no-one else is using direct database access to enter values. The only time in this project that identity insert is used is in the initial database deployment to setup specific values in lookup tables.
The QA team tried again right after getting the error, and was able to submit a query successfully, and they have been trying since then to reproduce it, and haven't succeeded so far.
I really do appreciate the ideas folks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
听起来身份种子已损坏或以某种方式重置。 最简单的解决方案是将种子重置为标识列的当前最大值:
Sounds like the identity seed got corrupted or reset somehow. Easiest solution will be to reset the seed to the current max value of the identity column:
虽然我没有对潜在原因的解释,但当然可以更改标识列的种子值。 如果种子被降低到表中已经存在下一个值的位置,那么这肯定会导致您所看到的情况。 尝试运行
DBCC CHECKIDENT (table_name)
并查看它会提供什么。有关详细信息,请查看 本页
While I don't have an explanation as to a potential cause, it is certinaly possible to change the seed value of an identity column. If the seed were lowered to where the next value would already exist in the table, then that could certainly cause what you're seeing. Try running
DBCC CHECKIDENT (table_name)
and see what it gives you.For more information, check out this page
基于经验的随机想法
您是否已与 Red Gate Data Compare 等工具同步数据。 它可以选择重新播种标识列。 这导致了使用问题。 上个月还有另一个项目。
您可能还显式加载/同步了 ID。
Random thought based on experience
Have you synched data with, say, Red Gate Data Compare. This has an option to reseed identity columns. It's caused issues for use. And another project last month.
You may also have explicitly loaded/synched IDs too.
也许有人直接使用新的ID显式地插入一些登录到服务器的记录,那么当身份自增字段达到这个数字时,就会发生主键违规。
但宇宙射线是一个很好的解释;)
Maybe someone insert some records logging into the server directly using a new ID explicity, then when the identity auto increment field reach this number a primary key violation happened.
But The cosmic ray is algo a good explanation ;)
只是为了非常非常确定...您没有在存储过程中使用 IDENTITY_INSERT 吗? 有些逻辑是这样的:
我只是打字就感觉很粘。 但是偶尔你会遇到一些人,他们从来没有完全理解身份栏的含义,我想确保排除这种情况。 顺便说一句:如果这就是答案,只要删除问题并且永远不承认这是你的问题,我就不会反对你!
你能看出我每年夏天都会雇用实习生吗?
Just to make very, very sure...you aren't using an IDENTITY_INSERT in your stored procedure are you? Some logic like this:
I feel sticky just typing it. But every once in awhile you run across folks that just never quite understood what an Identity column is all about and I want to make sure that this is ruled out. By the way: if this is the answer, I won't hold it against you if just delete the question and never admit that this was your problem!
Can you tell that I hire interns every summer?
您是否在任何程序中使用 @@identity 或scope_identity() 等函数? 如果您的表有触发器或多次插入,您可能会得到所需表的错误标识值
Are you using functions like @@identity or scope_identity() in any of your procedures? if your table has triggers or multiple inserts you could be getting back the wrong identity value for the table you want
希望情况并非如此,但 SQL 2005 中的 SCOPE_IDENTITY() 存在一个已知错误:
http://connect.microsoft.com/SQLServer/feedback/ViewFeedback。 aspx?FeedbackID=328811
Hopefully that is not the case, but there is a known bug in SQL 2005 with SCOPE_IDENTITY():
http://connect.microsoft.com/SQLServer/feedback/ViewFeedback.aspx?FeedbackID=328811
主键违规不一定来自该表。
应用程序是否触及任何其他表或调用该函数的任何其他存储过程? 桌子上有任何触发器吗? 或者存储过程本身是否使用任何其他表或存储过程?
特别是,审计表或触发器可能会导致这种情况。
The Primary Key violation is not necessarily coming from that table.
Does the application touch any other tables or call any other stored procedures for that function? Are there any triggers on the table? Or does the stored procedure itself use any other tables or stored procedures?
In particular, an Auditing table or trigger could cause this.