I am new to MERGE and I am sure that I have some error in my code.
This code will run and create my script:
I have two tables, one of which is called TempUpsert , which is populated from the SqlBulkCopy operation (100 million million records) and the Sales table, which contains production data that must be indexed and used.
I want to combine the TempUpsert table with Sales one
I obviously am doing something wrong as it fails even with the smallest example
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[TempUpsert]') ) drop table TempUpsert; CREATE TABLE [dbo].[TempUpsert]( [FirstName] [varchar](200) NOT NULL, [LastName] [varchar](200) NOT NULL, [Score] [int] NOT NULL ) ON [PRIMARY] ; CREATE TABLE [dbo].[Sales]( [FullName] [varchar](200) NOT NULL, [LastName] [varchar](200) NOT NULL, [FirstName] [varchar](200) NOT NULL, [lastUpdated] [date] NOT NULL, CONSTRAINT [PK_Sales] PRIMARY KEY CLUSTERED ( [FullName] ASC )
This returns:
(1 row (s) affected)
(Affected 1 row (s))
(Affected 1 row (s))
Msg 2627, Level 14, State 1, Procedure sp_MoveFromTempUpsert_to_Sales, Line 12
Violation of PRIMARY KEY "PK_Sales" restriction. Cannot insert duplicate key in object 'dbo.Sales'. Application completed.
(1 row (s) affected)
What am I doing wrong?
Very much appreciated
source share