I am trying to insert data from one table that was imported from an Excel file into another table with the same exact columns. When I go to insert all the data
INSERT INTO [NQL_RawData].[dbo].[WM_MFGPNs]
([Manufacturer],[MPNWP],[MFGPN],[Cage],[Url],[DataSheetUrl],[Description],[Status],[NRND],[RoHS],[PbFree],[LOT],[LeadTime],[AddedOn],[AddedBy])
SELECT [Manufacturer],dbo.BuildPNWP([MFGPN],0),[MFGPN],[Cage],[Url],[DataSheetUrl],[Description],[Status],[NRND],[RoHS],[PbFree],[LOT],[LeadTime],[AddedOn],[AddedBy]
FROM [NQL_RawData].[dbo].[wm_BulkImport] WHERE Manufacturer = 'MFG NAME'
it comes up with this error message
Msg 2627, Level 14, State 1, Line 1
Violation of PRIMARY KEY constraint ‘PK_MFGPNs_1’. Cannot insert duplicate key in object ‘dbo.WM_MFGPNs’.
The statement has been terminated.
So I changed the query to include WHERE NOT EXISTS
INSERT INTO [NQL_RawData].[dbo].[WM_MFGPNs]
([Manufacturer],[MPNWP],[MFGPN],[Cage],[Url],[DataSheetUrl],[Description],[Status],[NRND],[RoHS],[PbFree],[LOT],[LeadTime],[AddedOn],[AddedBy])
SELECT [Manufacturer],dbo.BuildPNWP([MFGPN],0),[MFGPN],[Cage],[Url],[DataSheetUrl],[Description],[Status],[NRND],[RoHS],[PbFree],[LOT],[LeadTime],[AddedOn],[AddedBy]
FROM [NQL_RawData].[dbo].[wm_BulkImport]
WHERE NOT EXISTS(
SELECT [MFGPN]
FROM [NQL_RawData].[dbo].[WM_MFGPNs]
WHERE Manufacturer = 'MFG NAME'
)
But I still receive the same error message. Any ideas?
At a wild guess, the PK of WM_MFGPNs isn’t Manufacturer, but another column, such as MFGPN.
Assuming this to be the case, your insert becomes
Edit : OK, Your table has a Composite Primary Key (i.e. the combination of 2 or more fields comprises a unique key).