插入不存在违反PRIMARY KEY

时间:2016-11-11 14:35:31

标签: sql

我在插入不存在的情况下遇到麻烦,我不确定MERGE声明是否会更有效或我的陈述有什么问题。

我已经存在View,需要将此View的新记录插入表中。

表格如下:

CREATE TABLE [dbo].[ser_number_all]
     (Serialnumber nvarchar(100) PRIMARY KEY,
     TypeName nvarchar(max),
     Date datetime,
     Parent_Serialnumber nvarchar(100),
     JobNumber nvarchar(30),
     ProductNode hierarchyid,
         );

Insert语句如下所示:

insert into [dbo].[ser_number_all]
  ( Serialnumber
  , TypeName
  , Date
  , Parent_Serialnumber
  , JobNumber
  , ProductNode)
  select    Serialnumber
          , TypeName
          , Date
          , Parent_Serialnumber
          , JobNumber
          , ProductNode
  from dbo.Hierachical_View_with_Jobnumbers as ser_number_all
where not exists (select 1
                  from Hierachical_View_with_Jobnumbers as hv
                  where hv. Serialnumber = ser_number_all.Serialnumber
                  and hv. TypeName = ser_number_all.TypeName
                  and hv. Date = ser_number_all.Date
                  and hv. Parent_Serialnumber = ser_number_all.Parent_Serialnumber
                  and hv. JobNumber = ser_number_all.JobNumber
                  and hv. ProductNode = ser_number_all.ProductNode);

只要View没有任何新记录,它看起来没问题,我没有收到任何错误,输出是0记录应该是。

当我向原始表添加新记录并且视图有1条记录时,我总是收到此错误:

    Msg 2627, Level 14, State 1, Line 4
Violation of PRIMARY KEY constraint 'PK__ser_numb__F2753A12C4ABA976'. Cannot insert duplicate key in object 'dbo.ser_number_all'. The duplicate key value is (.x3666AB05).
The statement has been terminated.

我不明白为什么它会在主键列中插入重复值,因为在我的WHERE子句中我看不到任何错误。

我也尝试使用IS NULL而不是ser_number_all.TypeName,并尝试使用NULL值但仍然相同的所有其他列。

同样,我来自Oracle,看起来我必须学习MS SQL与Oracle相比的许多不同之处。

欣赏任何建议:-) THX

编辑:

这里是View的代码:

CREATE VIEW [dbo].[Hierachical_View_with_Jobnumbers]
AS
WITH ProductList
AS
(
SELECT  p.Serialnumber, 
p.Type_Id,
p.Parent_Serialnumber, 
p.ActiveJob_Jobnumber as JobNumber,
N'/' + CONVERT(NVARCHAR(4000), ROW_NUMBER() OVER (ORDER BY p.Serialnumber)) + N'/' AS ProductNode_AsChar
FROM Products AS p
WHERE p.Parent_Serialnumber IS NULL

UNION ALL

SELECT  p.Serialnumber, 
p.Type_Id, 
p.Parent_Serialnumber, 
JobNumber,
pl.ProductNode_AsChar + CONVERT(NVARCHAR(4000), ROW_NUMBER() OVER (ORDER BY p.Serialnumber)) + N'/'
FROM Products AS p
INNER JOIN ProductList AS pl ON p.Parent_Serialnumber = pl.Serialnumber
)
SELECT  Serialnumber,
pt.Name as TypeName,
Parent_Serialnumber,
JobNumber,
CONVERT(HIERARCHYID, ProductNode_AsChar) AS ProductNode
FROM ProductList as pl
INNER JOIN ProductTypes as pt on pl.Type_Id = pt.Id;

1 个答案:

答案 0 :(得分:0)

@TheGameiswar

抱歉,现在我知道了你的意思;-)愚蠢的我......

这里的解决方案现在正常关联:

    insert into [dbo].[ser_number_all]
  ( Serialnumber
  , TypeName
  , Date
  , Parent_Serialnumber
  , JobNumber
  , ProductNode)
  select    Serialnumber
          , TypeName
          , Date
          , Parent_Serialnumber
          , JobNumber
          , ProductNode
  from dbo.Hierachical_View_with_Jobnumbers as hv
where not exists (select 1
                  from ser_number_all as sna
                  where hv. Serialnumber = sna.Serialnumber);

感谢大家的时间并指导我朝着正确的方向前进: - )