C # Does not execute stored procedure + trigger

0

Friends how are you!

I have a stored procedure that inserts records in a table, which I run from a WinForms application, I was making some modifications and I added a Trigger to the table to which the procedure inserts the data.

But already the procedure executed from WinForms is not able to insert the data if the table has the trigger, but if I remove the trigger to the table this procedure works again.

Trigger:

USE [BD_SICAP_AG_PRUEBAS]
GO
/****** Object:  Trigger [dbo].[TR_AGREGAR_DETALLE_SEGUIMIENTO]    Script Date: 14/06/2018 09:06:47 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER TRIGGER [dbo].[TR_AGREGAR_DETALLE_SEGUIMIENTO]
ON [dbo].[TDETALLE_PROGRAMACION] FOR INSERT
AS
DECLARE
@IdProgramacion int,
@FechaProgramacion date,
@NombreRuta varchar (150),
@PlacaVehiculo varchar (6),
@NombreConductor varchar (50),
@HoraSalida varchar (5),
@DiaAnterior char (1),
@HoraLlegadaProveedor varchar (5),
@Observaciones varchar (150)
SELECT @IdProgramacion=IdProgramacion, @FechaProgramacion = FechaProgramacion, 
@NombreRuta=NombreRuta,
@PlacaVehiculo=PlacaVehiculo,
@NombreConductor=NombreConductor,
@HoraSalida=HoraSalida,
@DiaAnterior=DiaAnterior,
@HoraLlegadaProveedor=HoraLlegadaProveedor,
@Observaciones=Observaciones FROM inserted
INSERT INTO TSEGUIMIENTO VALUES (@IdProgramacion, @FechaProgramacion, @NombreRuta, @PlacaVehiculo, 
@NombreConductor, @HoraSalida, @DiaAnterior,'','',@HoraLlegadaProveedor,'','','','','','','','','', @Observaciones,'')

Can you give me a guide to why this may happen?

Thank you very much !!

    
asked by Brian Velez 14.06.2018 в 16:22
source

1 answer

4

Currently the code of your trigger assumes that every time a row of data is modified (or inserted), it is not prepared for moments in which more than one is modified.

In the way you are using the trigger, the solution is very simple, skip the first step where you save the values in a variable and insert directly from the pseudo table INSERTED :

ALTER TRIGGER [dbo].[TR_AGREGAR_DETALLE_SEGUIMIENTO]
ON [dbo].[TDETALLE_PROGRAMACION] FOR INSERT
AS

INSERT INTO TSEGUIMIENTO 
SELECT  IdProgramacion, FechaProgramacion, NombreRuta, PlacaVehiculo, 
        NombreConductor,HoraSalida,DiaAnterior,'','',
        HoraLlegadaProveedor,'','','','','','','','','',Observaciones,'' 
FROM INSERTED
;
    
answered by 14.06.2018 / 16:48
source