In general, you should try to choose columns for your primary keys that never change, because changing them can be a pain for many reasons, one of which is a primary key constraint violation.
Why are you changing the CourseId (which is probably a PK in a Course table), and why are you changing it to a CourseId which already exists?
EDIT: Adding an answer
The problem with the accepted solution is that dropping and re-creating a clustered index (primary key) can be very time-consuming and problematic as explained here if you're dealing with tables of significant size.
A better solution would be to not insert duplicates in the first place. For example, if these are the only two columns in the table, you can simply update the records which don't cause a conflict, and then delete the ones that which will (because rows already exist with the set of values you want).
begin tran
create table #example (CourseId int, StudentId int)
insert into #example values (1,1), (1,2), (2,1)
declare @OldValue int, @NewValue int
set @OldValue = 1
set @NewValue = 2
-- Only update records which won't cause a conflict
update e
set CourseId = @NewValue
from #example e
left join #example e2
on (e.StudentId = e2.StudentId and e2.CourseId = @NewValue)
where e.CourseId = @OldValue
and e2.StudentId IS NULL
-- Delete the records which weren't updated
delete from #example
where CourseId = @OldValue
select * from #example
rollback