"违反PRIMARY KEY约束"插入时

时间:2012-09-10 10:12:05

标签: sql sql-server insert primary-key

我在其中一个表上运行插入时遇到上述错误。

AdmFormData有3个主键,FormType,KeyField和FieldName。

有人会有任何想法我做错了吗?

    set rowcount 0
    go



    INSERT INTO  AdmFormData
    (
    [FormType]
      ,[KeyField]
      ,[FieldName]
      ,[AlphaValue]
      ,[NumericValue]
      ,[DateValue]
    )
    SELECT
     'CUS' AS [FormType]
      ,Customer as [KeyField]
      ,'SAL001' AS [FieldName]
      ,[Customer Territory] AS [AlphaValue]
      ,NULL AS [NumericValue]
      ,NULL AS [DateValue]
    FROM
    dgl_territory a
    LEFT OUTER JOIN AdmFormData f
    on f.FormType = 'CUS' and f.FieldName = 'SAL001' and f.KeyField = a.Customer
    WHERE f.FormType IS NULL

1 个答案:

答案 0 :(得分:0)

错误告诉您,AdmFormData表中已有一条记录,其中包含您要尝试的主键INSERT

如果表格中的主键包含FormType, KeyField and FieldName,那么您已经拥有了包含值的记录:

'CUS' AS [FormType]
,Customer as [KeyField]
,'SAL001' AS [FieldName]

你不能拥有同一主键的那条记录,它们是唯一的。