CREATE TRIGGER LD0T0BJD
ON LD0P0BJD
AFTER DELETE, INSERT, UPDATE
AS
BEGIN
	SET NOCOUNT ON

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

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

	INSERT INTO LD0L0BJD (CSSANV,VYNAMN,BJDLNR,ANVID,JFRDTA1,JFRDTA2,JFRDTA3,JFRDTA4,JFRDTA10,JFRDTA11,JFRDTA12,JFRDTA13,UPDDAT,UPDSIGN,SYSSTARTTIME,SYSENDTIME)
	SELECT 
	CSSANV,VYNAMN,BJDLNR,ANVID,JFRDTA1,JFRDTA2,JFRDTA3,JFRDTA4,JFRDTA10,JFRDTA11,JFRDTA12,JFRDTA13,UPDDAT,UPDSIGN,@NOW, CAST('99991231' as datetime)
	FROM INSERTED
END
;