“Violation of PRIMARY KEY constraint” when Inserting
I'm getting the above error when running an insert into on one of our tables.
The AdmFormData has 3 primary keys, FormType, KeyField and FieldName.
Would anyone have any ideas what I'm doing wrong?
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
The error is telling you that you already have a record in the AdmFormData table that has the primary key you are trying to INSERT
If your primary key on the table contains FormType, KeyField and FieldName then you already have a record with the values:
'CUS' AS [FormType] ,Customer as [KeyField] ,'SAL001' AS [FieldName]
You cannot have more that one record with the same primary key, they are unique.