Reputation: 15
I have the following code in a cursor in SQL Server 2008 r2.
begin
if exists (select * from GestionIOF with (updlock,serializable) where Fecha
=@fechagestion and ID_Desc2 = @id_tipoalarmas and ID_Modulo = @id_modulo)
begin
update GestionIOF set Total = (Select Total from #Paso p where p.Fecha =
@fechagestion and p.Desc2 = @id_tipoalarmas and p.Modulo = @id_modulo),
Frecuencia = (Select Frecuencia from #Paso p where p.Fecha
= @fechagestion and p.Desc2 = @id_tipoalarmas
and p.Modulo = @id_modulo), Gestionado = 'False'
where GestionIOF.Fecha = @fechagestion and GestionIOF.ID_Desc2 =
@id_tipoalarmas and GestionIOF.ID_Modulo = @id_modulo
end
else
begin
INSERT INTO GestionIOF(ID_Area,ID_Controlador ,ID_Modulo
,ID_Desc2,ID_TipoEvento,Total,Frecuencia ,Fecha,Gestionado )
SELECT * FROM #Paso
end
I check if the primary key exists and then if it doesn't I insert it. But I am getting the following error:
Mens 2627, Nivel 14, Estado 1, Procedimiento InsertarGestionEC_IOF, Línea 67 Infracción de la restricción PRIMARY KEY 'PK_GestionIOF'. No se puede insertar una clave duplicada en el objeto 'dbo.GestionIOF'.
Maybe it is a bug. Any ideas?
Upvotes: 0
Views: 768
Reputation: 27324
The exists clause is checking based on some input parameters @id_tipoalarmas
and @id_modulo
- but the insert clause in the else part of the statement is using #paso
I would double check what is in #paso
, since that could have multiple rows, but your input parameters is just 1 set of IDS. #paso
could contain other ID's you are not checking.
Upvotes: 3