CREATE TRIGGER LD0T0DUT
ON LD0P0DUT
AFTER DELETE, INSERT, UPDATE
AS
BEGIN
	SET NOCOUNT ON

	DECLARE @NOW datetime2(7)
	SET @NOW = sysdatetime()

	UPDATE LD0L0DUT
	SET SYSENDTIME = @NOW
	FROM LD0L0BJD, DELETED
	WHERE LD0L0BJD.CSSANV = DELETED.GRUPPLNR
	AND LD0L0BJD.VYNAMN = DELETED.GRUPPANVID
	AND LD0L0BJD.SYSENDTIME=CAST('99991231' as datetime)

	INSERT INTO LD0L0DUT (GRUPPLNR,GRUPPANVID,UPDDAT,UPDSIGN,SYSSTARTTIME,SYSENDTIME)
	SELECT 
	GRUPPLNR,GRUPPANVID,UPDDAT,UPDSIGN,@NOW, CAST('99991231' as datetime)
	FROM INSERTED
END
;