SQL Server 2014 - 无法将值插入主键列,因为不存在"重复"

时间:2017-09-18 18:39:07

标签: sql-server

我有一个问题,乍一看看起来非常愚蠢(错误信息似乎足够描述),但实际上是某种错误或角落情况......我完全糊涂了

我有以下SQL插入代码

USE Broker2
GO

DBCC CHECKIDENT ('Broker2.dbo.Properties');
DBCC CHECKIDENT ('Broker2.dbo.Properties', RESEED, 9999);  
GO

SET IDENTITY_INSERT Broker2.dbo.Properties ON

INSERT INTO Broker2.dbo.Properties
(Id, IsActive, Phone, Phone2, [Address], ExhibitionPoleId, RegionId, PropertyTypeId, FurnishingId, Price, IsDeposited, DepositAmount, IsArchived, 
CreatedOn, IsDeleted, OfficeId, DealTypeId, AssignedTo, HasTransition, BrokerNote)
SELECT 
    convert(int, '10000' + convert(varchar, cp.Id)), 
    cp.is_active, 
    (select SUBSTRING(op.phone, 0, 11) from recrm_merged.dbo.contact_phones op where op.id = ccp.id) as phone,
    (select SUBSTRING(op.phone, 12, 35) from recrm_merged.dbo.contact_phones op where op.id = ccp.id) as phone2, 
    cp.[address],
    me.broker_id, 
    mr.Broker_Id, 
    mp.Broker_Id, 
    mf.Broker_Id, 
    cp.price, 
    cp.with_deposit, 
    0, 
    case when cp.property_status_id = 1 then 0 else 1 end as IsArchived,
    cp.created_at,
    0,
    2,
    md.Broker_Id,
    cp.bound_to_broker_id,
    0,
    (cp.[description] + '; ' + cp.furniture_details)
FROM recrm_merged.dbo.properties cp
    join recrm_merged.dbo.contacts cc on cp.contact_id = cc.Id
    join recrm_merged.dbo.contact_phones ccp on cc.id = ccp.contact_id
    join Broker2.dbo.map_exhibition_types me on cp.exposure_id = me.crm_id
    join Broker2.dbo.map_regions mr on cp.district_id = mr.Crm_Id
    join Broker2.dbo.map_property_types mp on cp.property_type_id = mp.Crm_Id
    join Broker2.dbo.map_furnishings mf on cp.furnishing_id = mf.Crm_Id
    join Broker2.dbo.map_deal_types md on cp.action_type_id = md.Crm_Id

SET IDENTITY_INSERT Broker2.dbo.Properties OFF

我也有这个表'属性',根据以下结果没有大于8469的ID

select top 1000 * from Properties
order by id desc -- results in 8469, 8468, 8467 ...

我从未收到此错误 - "违反PRIMARY KEY约束' PK_dbo.Properties'。无法在对象' dbo.Properties'中插入重复键。重复键值为(100003)。"

来自表recrm_merged.dbo.properties的第一个Id记录是3.我需要让新转移的ID以10000开始

我重新启动了Management Studio,PC并关闭了房间里的灯,但问题并没有消失......

如果有人扛起肩膀,那将会很受欢迎!

修改

select * from recrm_merged.dbo.properties
where Id = 3 -- returns only 1 record.

USE [Broker2]
GO

/****** Object:  Index [PK_dbo.Properties]    Script Date: 18.9.2017 г. 21:46:00 ******/
ALTER TABLE [dbo].[Properties] ADD  CONSTRAINT [PK_dbo.Properties] PRIMARY KEY CLUSTERED 
(
    [Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
GO

编辑2

实际上我只是发现我的Select会返回3条记录,这些记录会让我在腿上射击!这怎么可能

Id  is_active   phone   phone2  address broker_id   Broker_Id   Broker_Id   Broker_Id   price   with_deposit    (No column name)    IsArchived  created_at  (No column name)    (No column name)    Broker_Id   bound_to_broker_id  (No column name)    (No column name)
100003  0   0889525467      басанович 9 4   221 11  1   260 0   0   1   2016-05-05 17:30:32 0   2   1   NULL    0   ; 
100003  0   123123      басанович 9 4   221 11  1   260 0   0   1   2016-05-05 17:30:32 0   2   1   NULL    0   ; 
100003  0   aaaa        басанович 9 4   221 11  1   260 0   0   1   2016-05-05 17:30:32 0   2   1   NULL    0   ; 

2 个答案:

答案 0 :(得分:0)

请勿尝试设置密钥。更改" Id"标识规范下的表中的列为" Is Identity = Yes"。这将为您处理ID。然后你只需从插页中删除它,你就是金色的。然后,从您的一对多关系中返回的重复值不会让您崩溃。

答案 1 :(得分:0)

您的联接正在交叉倍增并创建重复项。您要加入的一个或多个表格中存在多对一关系,或者您的加入条件不完整。

尝试运行此功能以查找重复项:

SELECT cp.ID, COUNT(*)
FROM recrm_merged.dbo.properties cp
    join recrm_merged.dbo.contacts cc on cp.contact_id = cc.Id
    join recrm_merged.dbo.contact_phones ccp on cc.id = ccp.contact_id
    join Broker2.dbo.map_exhibition_types me on cp.exposure_id = me.crm_id
    join Broker2.dbo.map_regions mr on cp.district_id = mr.Crm_Id
    join Broker2.dbo.map_property_types mp on cp.property_type_id = mp.Crm_Id
    join Broker2.dbo.map_furnishings mf on cp.furnishing_id = mf.Crm_Id
    join Broker2.dbo.map_deal_types md on cp.action_type_id = md.Crm_Id
group by cp.ID
having count(*) > 1

从那里你必须挖掘并找出哪个连接正在创建重复项。

如果您确定所有记录都是cp.ID唯一的,那么您 可能能够使用distinct进行修复:

SELECT DISTINCT
    convert(int, '10000' + convert(varchar, cp.Id)), 
    cp.is_active, 
    (select SUBSTRING(op.phone, 0, 11) from recrm_merged.dbo.contact_phones op where op.id = ccp.id) as phone,
    (select SUBSTRING(op.phone, 12, 35) from recrm_merged.dbo.contact_phones op where op.id = ccp.id) as phone2, 
    cp.[address],
    me.broker_id, 
    mr.Broker_Id, 
    mp.Broker_Id, 
    mf.Broker_Id, 
    cp.price, 
    cp.with_deposit, 
    0, 
    case when cp.property_status_id = 1 then 0 else 1 end as IsArchived,
    cp.created_at,
    0,
    2,
    md.Broker_Id,
    cp.bound_to_broker_id,
    0,
    (cp.[description] + '; ' + cp.furniture_details)
FROM recrm_merged.dbo.properties cp
    join recrm_merged.dbo.contacts cc on cp.contact_id = cc.Id
    join recrm_merged.dbo.contact_phones ccp on cc.id = ccp.contact_id
    join Broker2.dbo.map_exhibition_types me on cp.exposure_id = me.crm_id
    join Broker2.dbo.map_regions mr on cp.district_id = mr.Crm_Id
    join Broker2.dbo.map_property_types mp on cp.property_type_id = mp.Crm_Id
    join Broker2.dbo.map_furnishings mf on cp.furnishing_id = mf.Crm_Id
    join Broker2.dbo.map_deal_types md on cp.action_type_id = md.Crm_Id

然而,在盲目地使用这种方法之前,你应该停下来理解为什么你会得到重复的东西。