我对创建的表有一个唯一约束:
ALTER TABLE dbo.AccessIntegrationSettings ADD CONSTRAINT ucAccessIntegrationSettings UNIQUE NONCLUSTERED
(
[AccessIntegrationPartnerSys] ASC,
[AccessIntegrationSettingEnum] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
目前,在表格中,只有一条记录,而且有问题的两列的值如下:
AccessIntegrationPartnerSys AccessIntegrationSettingEnum
------------------------------------------------------------
100000 1
现在,我正在尝试执行以下插入:
INSERT INTO AccessIntegrationSettings (AccessIntegrationPartnerSys, AccessIntegrationSettingEnum)
VALUES
(100000, 13)
但是我收到以下错误:
违反UNIQUE KEY约束'ucAccessIntegrationSettings'。无法在对象'dbo.AccessIntegrationSettings'中插入重复键。重复键值为(100000,
<NULL>
)。
我不明白为什么我会收到此错误。首先,我不是要插入重复的值。第二,我不确定为什么错误消息中的第二个值显示为NULL。
答案 0 :(得分:0)
我改为创建约束:
ALTER TABLE dbo.AccessIntegrationSettings ADD CONSTRAINT ucAccessIntegrationSettings UNIQUE NONCLUSTERED
(
[AccessIntegrationPartnerSys] ASC,
[AccessIntegrationSettingEnum] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
为:
IF NOT EXISTS(SELECT * FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE CONSTRAINT_NAME ='ucAccessIntegrationSettings')
ALTER TABLE dbo.AccessIntegrationSettings ADD CONSTRAINT ucAccessIntegrationSettings UNIQUE NONCLUSTERED
(
[AccessIntegrationPartnerSys] ASC,
[AccessIntegrationSettingEnum] ASC
)
这似乎解决了这个问题。我可以插入多行,但如果我尝试插入一个具有两个重复组合的行,则会抛出错误。我将不得不研究(或尝试)每个设置,以找出导致问题的原因,以及原因。