简体   繁体   English

我的第一个问题-违反PRIMARY KEY约束

[英]My first question — Violation of PRIMARY KEY constraint

I have the following issue: I want to execute a script where I'm trying to insert data from a database that I restored (iNode-8-24-14-Orig) into a database that I created. 我遇到以下问题:我想执行一个脚本,在其中尝试将我还原的数据库(iNode-8-24-14-Orig)中的数据插入到我创建的数据库中。

The code is: 代码是:

/****************************************************/
SET IDENTITY_INSERT [dbo].[XHQ_HIER_DEF] ON;
GO
INSERT INTO [dbo].[XHQ_HIER_DEF]
           ([HIER_KEY]
           ,[HIER_NAME]
           ,[HIER_DESC]
           ,[SYNONYM_DEF_LEVEL]
           ,[CRT_XHQUSERID]
           ,[CRT_TIMESTAMP]
           ,[CRT_TZ_BIAS]
           ,[UPDT_XHQUSERID]
           ,[UPDT_TIMESTAMP]
           ,[UPDT_TZ_BIAS])
SELECT [HIER_KEY]
      ,[HIER_NAME]
      ,[HIER_DESC]
      ,[SYNONYM_DEF_LEVEL]
      ,[CRT_XHQUSERID]
      ,[CRT_TIMESTAMP]
      ,[CRT_TZ_BIAS]
      ,[UPDT_XHQUSERID]
      ,[UPDT_TIMESTAMP]
      ,[UPDT_TZ_BIAS]
  FROM [iNode-8-24-14-Orig].[dbo].[XHQ_HIER_DEF]
GO
SET IDENTITY_INSERT [dbo].[XHQ_HIER_DEF] OFF;
GO
/****************************************************/

But I get this error for each table: 但我得到每个表此错误:

Violation of PRIMARY KEY constraint 'XPKXHQ_HIER_DEF'. 违反主键约束'XPKXHQ_HIER_DEF'。 Cannot insert duplicate key in object 'dbo.XHQ_HIER_DEF'. 无法在对象'dbo.XHQ_HIER_DEF'中插入重复的密钥。 The duplicate key value is (1). 重复键值为(1)。

Any idea how can I fix this? 知道我该如何解决吗? I want to mention that I have the same tables and columns in both databases. 我想提到的是,两个数据库中的表和列都相同。 I know is saying that I'm already using the same primary key, but I don't know how to fix it. 我知道我在说我已经在使用相同的主键,但是我不知道如何解决它。

This is my first question on this site. 这是我在本网站上的第一个问题。

And we care... why? 我们在乎...为什么?

Any idea how can i fix this? 任何想法我该如何解决?

Read the error, fix the data? 读取错误,修复数据? The error says very clear what the issue is: 该错误非常清楚地说明了问题所在:

Cannot insert duplicate key in object 'dbo.XHQ_HIER_DEF'. 无法在对象'dbo.XHQ_HIER_DEF'中插入重复的密钥。

Do not insert the same key value multiple times. 不要多次插入相同的键值。 Period. 期。 If you define a field as primary key, values have per definition to be unique in the key. 如果将字段定义为主键,则每个定义中的值在键中都是唯一的。

There already is an entry with identity 1, or your source data has multiple rows with the same value, which is not valid per your data model. 已经有一个标识为1的条目,或者您的源数据有多个具有相同值的行,这对于您的数据模型而言无效。

Generally for problems like this, actually reading the error helps. 通常对于此类问题,实际读取错误会有所帮助。 In your case it is EXTREMELY clear in the description what the issue is, even giving you the value causing the problem: 在您的情况下,在描述中非常清楚地指出了问题所在,甚至为您提供了引起问题的值:

The duplicate key value is (1). 重复键值为(1)。

Solution is obvious: do not insert duplicate primary key values. 解决方案显而易见:不要插入重复的主键值。

From your I suppose: 从你我想:

  1. You're using MSSQL with linked server 您正在将MSSQL与链接服务器一起使用
  2. PK consists of HIER_KEY field only PK仅包含HIER_KEY字段

So: 所以:

/****************************************************/
SET IDENTITY_INSERT [dbo].[XHQ_HIER_DEF] ON;
GO
INSERT INTO [dbo].[XHQ_HIER_DEF]
        ([HIER_KEY]
        ,[HIER_NAME]
        ,[HIER_DESC]
        ,[SYNONYM_DEF_LEVEL]
        ,[CRT_XHQUSERID]
        ,[CRT_TIMESTAMP]
        ,[CRT_TZ_BIAS]
        ,[UPDT_XHQUSERID]
        ,[UPDT_TIMESTAMP]
        ,[UPDT_TZ_BIAS])
SELECT [HIER_KEY]
    ,[HIER_NAME]
    ,[HIER_DESC]
    ,[SYNONYM_DEF_LEVEL]
    ,[CRT_XHQUSERID]
    ,[CRT_TIMESTAMP]
    ,[CRT_TZ_BIAS]
    ,[UPDT_XHQUSERID]
    ,[UPDT_TIMESTAMP]
    ,[UPDT_TZ_BIAS]
FROM [iNode-8-24-14-Orig].[dbo].[XHQ_HIER_DEF] T1 
WHERE NOT EXISTS(
    SELECT 1 FROM [dbo].[XHQ_HIER_DEF] T2 
        WHERE 
        T1.HIER_KEY = T2.HIER_KEY
)
GO
SET IDENTITY_INSERT [dbo].[XHQ_HIER_DEF] OFF;
GO
/****************************************************/

Warning: performance of such insert could be especially terrible. 警告:这种刀片的性能可能特别糟糕。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM