我正在使用 ssis 并且收到此错误“违反 PRIMARY KEY 约束。无法在对象中插入重复键。重复键值是

Nic*_*ick 0 sql-server ssis

我是 SSIS 的新手,我收到一条错误消息。谁能帮我 ?我的数据中没有重复项

错误消息是 OLE DB 记录可用。来源:“Microsoft SQL Server Native Client 11.0” Hresult:0x80040E2F 描述:“违反 PRIMARY KEY 约束 'PK_DimCourse'。无法在对象 'dbo.DimCourse' 中插入重复键。重复键值为 (CS1301)。”。

我当前的表看起来像这样

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[DimCourse](
    [CourseCode] [nvarchar](10) NOT NULL,
    [SubjectCode] [nvarchar](10) NOT NULL,
    [CourseNumber] [nvarchar](10) NOT NULL,
    [CourseTitle] [nvarchar](50) NOT NULL,
    [Level1] [nvarchar](20) NOT NULL,
    [Level2] [nvarchar](20) NOT NULL,
 CONSTRAINT [PK_DimCourse] PRIMARY KEY CLUSTERED 
(
    [CourseCode] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
Run Code Online (Sandbox Code Playgroud)

Gol*_*ish 5

I know I am too late to answer. I was getting the similar error message, found out what was the problem, and solved it. I hope it will help the future readers.

Scenario:

  1. I have a source table with dbo.Codes with primary key combination(CodeID, CodeName).
  2. In order to create my destination table, I have used the SQL Server-->Database-->Tasks-->Generate Scripts, option.
  3. Then, in the SSIS package, I simply used the OLE DB Source and OLE DB Destination element. It was returning error: "Violation of PRIMARY KEY constraint 'pkCodes'. Cannot insert duplicate key in object 'dbo.Codes'. The duplicate key value is (106, da7A).".

What I have tried to solve:

  1. I have tried to use the sql command as source: Select CodeID, CodeName from dbo.Codes GROUP BY CodeID, CodeName. It was still returning error. I got confused at this point.
  2. After searching online, I found a tips to add a SSIS Toolbox-->Common-->Sort element between OLE DB Source and OLE DB Destination. I checked the option "Remove rows with duplicate sort values" in the Sort element. I was still getting error.
  3. 然后,我启用了 OLE DB Source 和 Sort 元素之间的数据查看器。我可以看到源表中有两行:106, da7a 和 106, da7A。

真正的问题是什么?

我的源表 coulmn CodeName 区分大小写,但目标表中的 CodeName 列不区分大小写。出现这种情况是因为,sql server 生成脚本选项,脚本排序规则默认设置为 false。

对我有用的解决方案:

我使用脚本整理选项重新创建了我的目标表:True,这使我的目标列区分大小写,并且解决了我的问题。