I am having trouble with the code for an SQL Server Insert Into Query. The result gives an error message that there has been a "violation of primary key restraint". I have researched here and found that I need an If Not Exists statement within the code, but I haven't been able to get that code correct. I want to append records from one table to another. The example would be an 'EmployeeTable' Name, ReasonOff, Shiftdate, PositionID. The primary keys are PositionId and Shiftdate. I want to append records from that table to a TimeCard table when the ReasonOff equals 'S'. The code I used is:
Insert into [EmployeeTable] (Name, ReasonOff, Shiftdate, PositionID)
Select TimeCard.Name, TimeCard.ReasonOff, TimeCard.Shiftdate, TimeCard.PositionID
From TimeCard
Where (EmployeeTable.ReasonOff) = 'S';
Can you please help me with the If Not Exists code.
Insert into [EmployeeTable] (Name, ReasonOff, Shiftdate, PositionID)
Select TimeCard.Name, TimeCard.ReasonOff, TimeCard.Shiftdate, TimeCard.PositionID
From TimeCard
Where (EmployeeTable.ReasonOff) = 'S';
Can you please help me with the If Not Exists code.