In MSSQL Server, I have a table StudentCourse with a Composite Primary Key (StudentID, CourseID). I am trying to change the selected student to another course. One student record of each course group is preventing me to do
Violation of PRIMARY KEY constraint
'PK_StudentCourse'. Cannot insert
duplicate key in object
'StudentCourse'. The statement has
I found the problem. When I was building Conditions for query string, one condition wasn't adding GroupID criteria. The query string happens to miss that GroupID crieria when that record was included in query string. It was happening as follow.
UPDATE StudentCourse SET CourseID = 5 WHERE CourseID = 1 AND StudentID IN(2,3) UPDATE StudentCourse SET CourseID = 5 WHERE StudentID IN(1,2,3) UPDATE StudentCourse SET CourseID = 6 WHERE CourseID = 2 AND StudentID IN(2,3) UPDATE StudentCourse SET CourseID = 6 WHERE StudentID IN(2,3,4)
Of course, my query was violating primary key rule without CourseID criteria. Thanks for your time, mates.