-2
Hello I’m new still need to migrate some students to another table but gives the error of uplikey Dcate someone can help ?
INSERT INTO [base_order_corp].[dbo].LY_HISTMATRICULA(ALUNO, ordem, ano, semestre, disciplina, SITUACAO_HIST,NIVEL_PRESENCA ,creditos)
SELECT A.ALUNO , 2 ORDEM , 2021 ANO , 1 SEMESTRE , 'APCRED' DISCIPLINA , 'Dispensado' SITUACAO_HIST , 'Presencial' NIVEL_PRESENCA , AP.CREDITOS
FROM LY_ALUNO A INNER JOIN LY_APROVEITAMENTO_CREDITO AP
on AP.ALUNO = A.ALUNO
and AP.CURSO = A.CURSO
and AP.TURNO = A.TURNO
and AP.CURRICULO = A.CURRICULO
and A.ALUNO = AP.ALUNO
WHERE
A.CURSO like 'cga%'
and A.SIT_ALUNO = 'Ativo'
and A.SERIE = 8
and not exists (select 'x' from [base_order_corp].[dbo].LY_HISTMATRICULA H WHERE H.ALUNO = A.ALUNO and H.DISCIPLINA = 'APCRED'and H.ANO = 2021 and H.SEMESTRE = 1 and H.ORDEM = 2)
the most obvious answer is "do not enter duplicate values" :) in your query you have a
not exists
there and that should solve the problem, what should be spoiling are the other criteria in thewhere
, thatnot exists
should only be to remove what already exists and eveitar duplicates. Move the criteria to thewhere
outside, and leave onwhere
of that subselect only the primary key field, which should probably be "student"– Ricardo Pontual
All that in Where are keys Msg 2627, Level 14, State 1, Procedure Cro_ly_histmatricula_insert, Line 123 Violation of PRIMARY KEY Constraint 'PK_LY_HISTMATRICULA'. Cannot Insert Duplicate key in Object 'dbo.LY_HISTMATRICULA'. The Duplicate key value is (111170200055, 2, 2021, 1, APCRED). The statement has been terminated.
– Guilherme Ribeiro