

CREATE TABLE "ceRem3_sync" (
	"id" INT NOT NULL IDENTITY(1,1) PRIMARY KEY,
	"command" NVARCHAR(50) NOT NULL,
	"table" NVARCHAR(50) NOT NULL,
	"key" NVARCHAR(50) NOT NULL,
	"datum" DATETIME NOT NULL
);



DROP TRIGGER "ceRem3_ADRESS";
DROP TRIGGER "ceRem3_ARPREIS";
DROP TRIGGER "ceRem3_ART";
DROP TRIGGER "ceRem3_BELEG";
DROP TRIGGER "ceRem3_BELEGP";
DROP TRIGGER "ceRem3_BILD";
DROP TRIGGER "ceRem3_CRM_ADRESSEN";
DROP TRIGGER "ceRem3_DEBITOREN";
DROP TRIGGER "ceRem3_RABGRUP";
DROP TRIGGER "ceRem3_WAWIINTERESS";
DROP TRIGGER "ceRem3_WPROJEKT";
DROP TRIGGER "ceRem3_ZUBEHOER";







CREATE TRIGGER "ceRem3_BELEGP"
ON "BELEGP" AFTER INSERT, DELETE, UPDATE
AS
BEGIN
	DECLARE 
		@erp_tbl AS VARCHAR(50) = 'BELEGP',
		@cmd AS VARCHAR(50),
		@q AS VARCHAR(MAX)
	
	SELECT [BELEGP_ID] AS [key] INTO "#trig_ins" FROM [INSERTED]
	SELECT [BELEGP_ID] AS [key] INTO "#trig_del" FROM [DELETED]
	
	CREATE TABLE "#trig_tbl" ([key] INT)
	
	IF NOT EXISTS (SELECT * FROM INSERTED) AND EXISTS (SELECT * FROM DELETED)
	BEGIN
		SET @cmd = 'DELETE'
		INSERT INTO "#trig_tbl" SELECT [key] FROM "#trig_del"
		DELETE [s] FROM [ceRem3_sync] [s] LEFT JOIN "#trig_tbl" [t] ON [s].[key]=[t].[key] AND [s].[table]=@erp_tbl WHERE [t].[key] IS NOT NULL
	END
	
	IF EXISTS (SELECT * FROM INSERTED) AND NOT EXISTS (SELECT * FROM DELETED)
	BEGIN
		SET @cmd = 'INSERT'
		INSERT INTO "#trig_tbl" SELECT [key] FROM "#trig_ins"
		DELETE [s] FROM [ceRem3_sync] [s] LEFT JOIN "#trig_tbl" [t] ON [s].[key]=[t].[key] AND [s].[table]=@erp_tbl WHERE [t].[key] IS NOT NULL
	END
	
	IF EXISTS (SELECT * FROM INSERTED) AND EXISTS (SELECT * FROM DELETED)
	BEGIN
		SET @cmd = 'UPDATE'
		INSERT INTO "#trig_tbl" SELECT [key] FROM "#trig_ins"
		DELETE [s] FROM [ceRem3_sync] [s] LEFT JOIN "#trig_tbl" [t] ON [s].[key]=[t].[key] AND [s].[table]=@erp_tbl WHERE [t].[key] IS NOT NULL
	END
	
	INSERT INTO [ceRem3_sync] ([command], [table], [key], [datum])
		SELECT @cmd, @erp_tbl, [t].[key], GETDATE() 
			FROM "#trig_tbl" [t]
			LEFT JOIN [ceRem3_sync] [s] ON [s].[key]=[t].[key] AND [s].[table]=@erp_tbl
			WHERE [s].[key] IS NULL
	
	DROP TABLE #trig_ins
	DROP TABLE #trig_del
	DROP TABLE #trig_tbl
END;





CREATE TRIGGER "ceRem3_BELEG"
ON "BELEG" AFTER INSERT, DELETE, UPDATE
AS
BEGIN
	DECLARE 
		@erp_tbl AS VARCHAR(50) = 'BELEG',
		@cmd AS VARCHAR(50),
		@q AS VARCHAR(MAX)
	
	SELECT [BELEG_ID] AS [key] INTO "#trig_ins" FROM [INSERTED]
	SELECT [BELEG_ID] AS [key] INTO "#trig_del" FROM [DELETED]
	
	CREATE TABLE "#trig_tbl" ([key] INT)
	
	IF NOT EXISTS (SELECT * FROM INSERTED) AND EXISTS (SELECT * FROM DELETED)
	BEGIN
		SET @cmd = 'DELETE'
		INSERT INTO "#trig_tbl" SELECT [key] FROM "#trig_del"
		DELETE [s] FROM [ceRem3_sync] [s] LEFT JOIN "#trig_tbl" [t] ON [s].[key]=[t].[key] AND [s].[table]=@erp_tbl WHERE [t].[key] IS NOT NULL
	END
	
	IF EXISTS (SELECT * FROM INSERTED) AND NOT EXISTS (SELECT * FROM DELETED)
	BEGIN
		SET @cmd = 'INSERT'
		INSERT INTO "#trig_tbl" SELECT [key] FROM "#trig_ins"
		DELETE [s] FROM [ceRem3_sync] [s] LEFT JOIN "#trig_tbl" [t] ON [s].[key]=[t].[key] AND [s].[table]=@erp_tbl WHERE [t].[key] IS NOT NULL
	END
	
	IF EXISTS (SELECT * FROM INSERTED) AND EXISTS (SELECT * FROM DELETED)
	BEGIN
		SET @cmd = 'UPDATE'
		INSERT INTO "#trig_tbl" SELECT [key] FROM "#trig_ins"
		DELETE [s] FROM [ceRem3_sync] [s] LEFT JOIN "#trig_tbl" [t] ON [s].[key]=[t].[key] AND [s].[table]=@erp_tbl WHERE [t].[key] IS NOT NULL
	END
	
	INSERT INTO [ceRem3_sync] ([command], [table], [key], [datum])
		SELECT @cmd, @erp_tbl, [t].[key], GETDATE() 
			FROM "#trig_tbl" [t]
			LEFT JOIN [ceRem3_sync] [s] ON [s].[key]=[t].[key] AND [s].[table]=@erp_tbl
			WHERE [s].[key] IS NULL
	
	DROP TABLE #trig_ins
	DROP TABLE #trig_del
	DROP TABLE #trig_tbl
END;





CREATE TRIGGER "ceRem3_DEBITOREN"
ON "DEBITOREN" AFTER INSERT, DELETE, UPDATE
AS
BEGIN
	DECLARE 
		@erp_tbl AS VARCHAR(50) = 'DEBITOREN',
		@cmd AS VARCHAR(50),
		@q AS VARCHAR(MAX)
	
	SELECT [KUNDEN_ID] AS [key] INTO "#trig_ins" FROM [INSERTED]
	SELECT [KUNDEN_ID] AS [key] INTO "#trig_del" FROM [DELETED]
	
	CREATE TABLE "#trig_tbl" ([key] INT)
	
	IF NOT EXISTS (SELECT * FROM INSERTED) AND EXISTS (SELECT * FROM DELETED)
	BEGIN
		SET @cmd = 'DELETE'
		INSERT INTO "#trig_tbl" SELECT [key] FROM "#trig_del"
		DELETE [s] FROM [ceRem3_sync] [s] LEFT JOIN "#trig_tbl" [t] ON [s].[key]=[t].[key] AND [s].[table]=@erp_tbl WHERE [t].[key] IS NOT NULL
	END
	
	IF EXISTS (SELECT * FROM INSERTED) AND NOT EXISTS (SELECT * FROM DELETED)
	BEGIN
		SET @cmd = 'INSERT'
		INSERT INTO "#trig_tbl" SELECT [key] FROM "#trig_ins"
		DELETE [s] FROM [ceRem3_sync] [s] LEFT JOIN "#trig_tbl" [t] ON [s].[key]=[t].[key] AND [s].[table]=@erp_tbl WHERE [t].[key] IS NOT NULL
	END
	
	IF EXISTS (SELECT * FROM INSERTED) AND EXISTS (SELECT * FROM DELETED)
	BEGIN
		SET @cmd = 'UPDATE'
		INSERT INTO "#trig_tbl" SELECT [key] FROM "#trig_ins"
		DELETE [s] FROM [ceRem3_sync] [s] LEFT JOIN "#trig_tbl" [t] ON [s].[key]=[t].[key] AND [s].[table]=@erp_tbl WHERE [t].[key] IS NOT NULL
	END
	
	INSERT INTO [ceRem3_sync] ([command], [table], [key], [datum])
		SELECT @cmd, @erp_tbl, [t].[key], GETDATE() 
			FROM "#trig_tbl" [t]
			LEFT JOIN [ceRem3_sync] [s] ON [s].[key]=[t].[key] AND [s].[table]=@erp_tbl
			WHERE [s].[key] IS NULL
	
	DROP TABLE #trig_ins
	DROP TABLE #trig_del
	DROP TABLE #trig_tbl
END;





CREATE TRIGGER "ceRem3_ART"
ON "ART" AFTER INSERT, DELETE, UPDATE
AS
BEGIN
	DECLARE 
		@erp_tbl AS VARCHAR(50) = 'ART',
		@cmd AS VARCHAR(50),
		@q AS VARCHAR(MAX)
	
	SELECT [ART_ID] AS [key] INTO "#trig_ins" FROM [INSERTED]
	SELECT [ART_ID] AS [key] INTO "#trig_del" FROM [DELETED]
	
	CREATE TABLE "#trig_tbl" ([key] INT)
	
	IF NOT EXISTS (SELECT * FROM INSERTED) AND EXISTS (SELECT * FROM DELETED)
	BEGIN
		SET @cmd = 'DELETE'
		INSERT INTO "#trig_tbl" SELECT [key] FROM "#trig_del"
		DELETE [s] FROM [ceRem3_sync] [s] LEFT JOIN "#trig_tbl" [t] ON [s].[key]=[t].[key] AND [s].[table]=@erp_tbl WHERE [t].[key] IS NOT NULL
	END
	
	IF EXISTS (SELECT * FROM INSERTED) AND NOT EXISTS (SELECT * FROM DELETED)
	BEGIN
		SET @cmd = 'INSERT'
		INSERT INTO "#trig_tbl" SELECT [key] FROM "#trig_ins"
		DELETE [s] FROM [ceRem3_sync] [s] LEFT JOIN "#trig_tbl" [t] ON [s].[key]=[t].[key] AND [s].[table]=@erp_tbl WHERE [t].[key] IS NOT NULL
	END
	
	IF EXISTS (SELECT * FROM INSERTED) AND EXISTS (SELECT * FROM DELETED)
	BEGIN
		SET @cmd = 'UPDATE'
		INSERT INTO "#trig_tbl" SELECT [key] FROM "#trig_ins"
		DELETE [s] FROM [ceRem3_sync] [s] LEFT JOIN "#trig_tbl" [t] ON [s].[key]=[t].[key] AND [s].[table]=@erp_tbl WHERE [t].[key] IS NOT NULL
	END
	
	INSERT INTO [ceRem3_sync] ([command], [table], [key], [datum])
		SELECT @cmd, @erp_tbl, [t].[key], GETDATE() 
			FROM "#trig_tbl" [t]
			LEFT JOIN [ceRem3_sync] [s] ON [s].[key]=[t].[key] AND [s].[table]=@erp_tbl
			WHERE [s].[key] IS NULL
	
	DROP TABLE #trig_ins
	DROP TABLE #trig_del
	DROP TABLE #trig_tbl
END;





CREATE TRIGGER "ceRem3_WPROJEKT"
ON "WPROJEKT" AFTER INSERT, DELETE, UPDATE
AS
BEGIN
	DECLARE 
		@erp_tbl AS VARCHAR(50) = 'WPROJEKT',
		@cmd AS VARCHAR(50),
		@q AS VARCHAR(MAX)
	
	SELECT [WPROJEKT_ID] AS [key] INTO "#trig_ins" FROM [INSERTED]
	SELECT [WPROJEKT_ID] AS [key] INTO "#trig_del" FROM [DELETED]
	
	CREATE TABLE "#trig_tbl" ([key] INT)
	
	IF NOT EXISTS (SELECT * FROM INSERTED) AND EXISTS (SELECT * FROM DELETED)
	BEGIN
		SET @cmd = 'DELETE'
		INSERT INTO "#trig_tbl" SELECT [key] FROM "#trig_del"
		DELETE [s] FROM [ceRem3_sync] [s] LEFT JOIN "#trig_tbl" [t] ON [s].[key]=[t].[key] AND [s].[table]=@erp_tbl WHERE [t].[key] IS NOT NULL
	END
	
	IF EXISTS (SELECT * FROM INSERTED) AND NOT EXISTS (SELECT * FROM DELETED)
	BEGIN
		SET @cmd = 'INSERT'
		DELETE [s] FROM [ceRem3_sync] [s] LEFT JOIN "#trig_tbl" [t] ON [s].[key]=[t].[key] AND [s].[table]=@erp_tbl WHERE [t].[key] IS NOT NULL
	END
	
	IF EXISTS (SELECT * FROM INSERTED) AND EXISTS (SELECT * FROM DELETED)
	BEGIN
		SET @cmd = 'UPDATE'
		INSERT INTO "#trig_tbl" SELECT [key] FROM "#trig_ins"
		DELETE [s] FROM [ceRem3_sync] [s] LEFT JOIN "#trig_tbl" [t] ON [s].[key]=[t].[key] AND [s].[table]=@erp_tbl WHERE [t].[key] IS NOT NULL
	END
	
	INSERT INTO [ceRem3_sync] ([command], [table], [key], [datum])
		SELECT @cmd, @erp_tbl, [t].[key], GETDATE() 
			FROM "#trig_tbl" [t]
			LEFT JOIN [ceRem3_sync] [s] ON [s].[key]=[t].[key] AND [s].[table]=@erp_tbl
			WHERE [s].[key] IS NULL
	
	DROP TABLE #trig_ins
	DROP TABLE #trig_del
	DROP TABLE #trig_tbl
END;





CREATE TRIGGER "ceRem3_BILD"
ON "BILD" AFTER INSERT, DELETE, UPDATE
AS
BEGIN
	DECLARE 
		@erp_tbl AS VARCHAR(50) = 'BILD',
		@cmd AS VARCHAR(50),
		@q AS VARCHAR(MAX)
	
	SELECT [BILD_ID] AS [key] INTO "#trig_ins" FROM [INSERTED]
	SELECT [BILD_ID] AS [key] INTO "#trig_del" FROM [DELETED]
	
	CREATE TABLE "#trig_tbl" ([key] INT)
	
	IF NOT EXISTS (SELECT * FROM INSERTED) AND EXISTS (SELECT * FROM DELETED)
	BEGIN
		SET @cmd = 'DELETE'
		INSERT INTO "#trig_tbl" SELECT [key] FROM "#trig_del"
		DELETE [s] FROM [ceRem3_sync] [s] LEFT JOIN "#trig_tbl" [t] ON [s].[key]=[t].[key] AND [s].[table]=@erp_tbl WHERE [t].[key] IS NOT NULL
	END
	
	IF EXISTS (SELECT * FROM INSERTED) AND NOT EXISTS (SELECT * FROM DELETED)
	BEGIN
		SET @cmd = 'INSERT'
		INSERT INTO "#trig_tbl" SELECT [key] FROM "#trig_ins"
		DELETE [s] FROM [ceRem3_sync] [s] LEFT JOIN "#trig_tbl" [t] ON [s].[key]=[t].[key] AND [s].[table]=@erp_tbl WHERE [t].[key] IS NOT NULL
	END
	
	IF EXISTS (SELECT * FROM INSERTED) AND EXISTS (SELECT * FROM DELETED)
	BEGIN
		SET @cmd = 'UPDATE'
		INSERT INTO "#trig_tbl" SELECT [key] FROM "#trig_ins"
		DELETE [s] FROM [ceRem3_sync] [s] LEFT JOIN "#trig_tbl" [t] ON [s].[key]=[t].[key] AND [s].[table]=@erp_tbl WHERE [t].[key] IS NOT NULL
	END
	
	INSERT INTO [ceRem3_sync] ([command], [table], [key], [datum])
		SELECT @cmd, @erp_tbl, [t].[key], GETDATE() 
			FROM "#trig_tbl" [t]
			LEFT JOIN [ceRem3_sync] [s] ON [s].[key]=[t].[key] AND [s].[table]=@erp_tbl
			WHERE [s].[key] IS NULL
	
	DROP TABLE #trig_ins
	DROP TABLE #trig_del
	DROP TABLE #trig_tbl
END;





CREATE TRIGGER "ceRem3_ARPREIS"
ON "ARPREIS" AFTER INSERT, DELETE, UPDATE
AS
BEGIN
	DECLARE 
		@erp_tbl AS VARCHAR(50) = 'ARPREIS',
		@cmd AS VARCHAR(50),
		@q AS VARCHAR(MAX)
	
	SELECT [ARPREIS_ID] AS [key] INTO "#trig_ins" FROM [INSERTED]
	SELECT [ARPREIS_ID] AS [key] INTO "#trig_del" FROM [DELETED]
	
	CREATE TABLE "#trig_tbl" ([key] INT)
	
	IF NOT EXISTS (SELECT * FROM INSERTED) AND EXISTS (SELECT * FROM DELETED)
	BEGIN
		SET @cmd = 'DELETE'
		INSERT INTO "#trig_tbl" SELECT [key] FROM "#trig_del"
		DELETE [s] FROM [ceRem3_sync] [s] LEFT JOIN "#trig_tbl" [t] ON [s].[key]=[t].[key] AND [s].[table]=@erp_tbl WHERE [t].[key] IS NOT NULL
	END
	
	IF EXISTS (SELECT * FROM INSERTED) AND NOT EXISTS (SELECT * FROM DELETED)
	BEGIN
		SET @cmd = 'INSERT'
		INSERT INTO "#trig_tbl" SELECT [key] FROM "#trig_ins"
		DELETE [s] FROM [ceRem3_sync] [s] LEFT JOIN "#trig_tbl" [t] ON [s].[key]=[t].[key] AND [s].[table]=@erp_tbl WHERE [t].[key] IS NOT NULL
	END
	
	IF EXISTS (SELECT * FROM INSERTED) AND EXISTS (SELECT * FROM DELETED)
	BEGIN
		SET @cmd = 'UPDATE'
		INSERT INTO "#trig_tbl" SELECT [key] FROM "#trig_ins"
		DELETE [s] FROM [ceRem3_sync] [s] LEFT JOIN "#trig_tbl" [t] ON [s].[key]=[t].[key] AND [s].[table]=@erp_tbl WHERE [t].[key] IS NOT NULL
	END
	
	INSERT INTO [ceRem3_sync] ([command], [table], [key], [datum])
		SELECT @cmd, @erp_tbl, [t].[key], GETDATE() 
			FROM "#trig_tbl" [t]
			LEFT JOIN [ceRem3_sync] [s] ON [s].[key]=[t].[key] AND [s].[table]=@erp_tbl
			WHERE [s].[key] IS NULL
	
	DROP TABLE #trig_ins
	DROP TABLE #trig_del
	DROP TABLE #trig_tbl
END;





CREATE TRIGGER "ceRem3_RABGRUP"
ON "RABGRUP" AFTER INSERT, DELETE, UPDATE
AS
BEGIN
	DECLARE 
		@erp_tbl AS VARCHAR(50) = 'RABGRUP',
		@cmd AS VARCHAR(50),
		@q AS VARCHAR(MAX)
	
	SELECT [RABGRUP_ID] AS [key] INTO "#trig_ins" FROM [INSERTED]
	SELECT [RABGRUP_ID] AS [key] INTO "#trig_del" FROM [DELETED]
	
	CREATE TABLE "#trig_tbl" ([key] INT)
	
	IF NOT EXISTS (SELECT * FROM INSERTED) AND EXISTS (SELECT * FROM DELETED)
	BEGIN
		SET @cmd = 'DELETE'
		INSERT INTO "#trig_tbl" SELECT [key] FROM "#trig_del"
		DELETE [s] FROM [ceRem3_sync] [s] LEFT JOIN "#trig_tbl" [t] ON [s].[key]=[t].[key] AND [s].[table]=@erp_tbl WHERE [t].[key] IS NOT NULL
	END
	
	IF EXISTS (SELECT * FROM INSERTED) AND NOT EXISTS (SELECT * FROM DELETED)
	BEGIN
		SET @cmd = 'INSERT'
		INSERT INTO "#trig_tbl" SELECT [key] FROM "#trig_ins"
		DELETE [s] FROM [ceRem3_sync] [s] LEFT JOIN "#trig_tbl" [t] ON [s].[key]=[t].[key] AND [s].[table]=@erp_tbl WHERE [t].[key] IS NOT NULL
	END
	
	IF EXISTS (SELECT * FROM INSERTED) AND EXISTS (SELECT * FROM DELETED)
	BEGIN
		SET @cmd = 'UPDATE'
		INSERT INTO "#trig_tbl" SELECT [key] FROM "#trig_ins"
		DELETE [s] FROM [ceRem3_sync] [s] LEFT JOIN "#trig_tbl" [t] ON [s].[key]=[t].[key] AND [s].[table]=@erp_tbl WHERE [t].[key] IS NOT NULL
	END
	
	INSERT INTO [ceRem3_sync] ([command], [table], [key], [datum])
		SELECT @cmd, @erp_tbl, [t].[key], GETDATE() 
			FROM "#trig_tbl" [t]
			LEFT JOIN [ceRem3_sync] [s] ON [s].[key]=[t].[key] AND [s].[table]=@erp_tbl
			WHERE [s].[key] IS NULL
	
	DROP TABLE #trig_ins
	DROP TABLE #trig_del
	DROP TABLE #trig_tbl
END;





CREATE TRIGGER "ceRem3_ZUBEHOER"
ON "ZUBEHOER" AFTER INSERT, DELETE, UPDATE
AS
BEGIN
	DECLARE 
		@erp_tbl AS VARCHAR(50) = 'ZUBEHOER',
		@cmd AS VARCHAR(50),
		@q AS VARCHAR(MAX)
	
	SELECT [ZUBEHOER_ID] AS [key] INTO "#trig_ins" FROM [INSERTED]
	SELECT [ZUBEHOER_ID] AS [key] INTO "#trig_del" FROM [DELETED]
	
	CREATE TABLE "#trig_tbl" ([key] INT)
	
	IF NOT EXISTS (SELECT * FROM INSERTED) AND EXISTS (SELECT * FROM DELETED)
	BEGIN
		SET @cmd = 'DELETE'
		INSERT INTO "#trig_tbl" SELECT [key] FROM "#trig_del"
		DELETE [s] FROM [ceRem3_sync] [s] LEFT JOIN "#trig_tbl" [t] ON [s].[key]=[t].[key] AND [s].[table]=@erp_tbl WHERE [t].[key] IS NOT NULL
	END
	
	IF EXISTS (SELECT * FROM INSERTED) AND NOT EXISTS (SELECT * FROM DELETED)
	BEGIN
		SET @cmd = 'INSERT'
		DELETE [s] FROM [ceRem3_sync] [s] LEFT JOIN "#trig_tbl" [t] ON [s].[key]=[t].[key] AND [s].[table]=@erp_tbl WHERE [t].[key] IS NOT NULL
	END
	
	IF EXISTS (SELECT * FROM INSERTED) AND EXISTS (SELECT * FROM DELETED)
	BEGIN
		SET @cmd = 'UPDATE'
		DELETE [s] FROM [ceRem3_sync] [s] LEFT JOIN "#trig_tbl" [t] ON [s].[key]=[t].[key] AND [s].[table]=@erp_tbl WHERE [t].[key] IS NOT NULL
	END
	
	INSERT INTO [ceRem3_sync] ([command], [table], [key], [datum])
		SELECT @cmd, @erp_tbl, [t].[key], GETDATE() 
			FROM "#trig_tbl" [t]
			LEFT JOIN [ceRem3_sync] [s] ON [s].[key]=[t].[key] AND [s].[table]=@erp_tbl
			WHERE [s].[key] IS NULL
	
	DROP TABLE #trig_ins
	DROP TABLE #trig_del
	DROP TABLE #trig_tbl
END;





CREATE TRIGGER "ceRem3_ADRESS"
ON "ADRESS" AFTER INSERT, DELETE, UPDATE
AS
BEGIN
	DECLARE 
		@erp_tbl AS VARCHAR(50) = 'ADRESS',
		@cmd AS VARCHAR(50),
		@q AS VARCHAR(MAX)
	
	SELECT [ADRESS_ID] AS [key] INTO "#trig_ins" FROM [INSERTED]
	SELECT [ADRESS_ID] AS [key] INTO "#trig_del" FROM [DELETED]
	
	CREATE TABLE "#trig_tbl" ([key] INT)
	
	IF NOT EXISTS (SELECT * FROM INSERTED) AND EXISTS (SELECT * FROM DELETED)
	BEGIN
		SET @cmd = 'DELETE'
		INSERT INTO "#trig_tbl" SELECT [key] FROM "#trig_del"
		DELETE [s] FROM [ceRem3_sync] [s] LEFT JOIN "#trig_tbl" [t] ON [s].[key]=[t].[key] AND [s].[table]=@erp_tbl WHERE [t].[key] IS NOT NULL
	END
	
	IF EXISTS (SELECT * FROM INSERTED) AND NOT EXISTS (SELECT * FROM DELETED)
	BEGIN
		SET @cmd = 'INSERT'
		INSERT INTO "#trig_tbl" SELECT [key] FROM "#trig_ins"
		DELETE [s] FROM [ceRem3_sync] [s] LEFT JOIN "#trig_tbl" [t] ON [s].[key]=[t].[key] AND [s].[table]=@erp_tbl WHERE [t].[key] IS NOT NULL
	END
	
	IF EXISTS (SELECT * FROM INSERTED) AND EXISTS (SELECT * FROM DELETED)
	BEGIN
		SET @cmd = 'UPDATE'
		INSERT INTO "#trig_tbl" SELECT [key] FROM "#trig_ins"
		DELETE [s] FROM [ceRem3_sync] [s] LEFT JOIN "#trig_tbl" [t] ON [s].[key]=[t].[key] AND [s].[table]=@erp_tbl WHERE [t].[key] IS NOT NULL
	END
	
	INSERT INTO [ceRem3_sync] ([command], [table], [key], [datum])
		SELECT @cmd, @erp_tbl, [t].[key], GETDATE() 
			FROM "#trig_tbl" [t]
			LEFT JOIN [ceRem3_sync] [s] ON [s].[key]=[t].[key] AND [s].[table]=@erp_tbl
			WHERE [s].[key] IS NULL
	
	DROP TABLE #trig_ins
	DROP TABLE #trig_del
	DROP TABLE #trig_tbl
END;





CREATE TRIGGER "ceRem3_WAWIINTERESS"
ON "WAWIINTERESS" AFTER INSERT, DELETE, UPDATE
AS
BEGIN
	DECLARE 
		@erp_tbl AS VARCHAR(50) = 'WAWIINTERESS',
		@cmd AS VARCHAR(50),
		@q AS VARCHAR(MAX)
	
	SELECT [INTERESS_ID] AS [key] INTO "#trig_ins" FROM [INSERTED]
	SELECT [INTERESS_ID] AS [key] INTO "#trig_del" FROM [DELETED]
	
	CREATE TABLE "#trig_tbl" ([key] INT)
	
	IF NOT EXISTS (SELECT * FROM INSERTED) AND EXISTS (SELECT * FROM DELETED)
	BEGIN
		SET @cmd = 'DELETE'
		INSERT INTO "#trig_tbl" SELECT [key] FROM "#trig_del"
		DELETE [s] FROM [ceRem3_sync] [s] LEFT JOIN "#trig_tbl" [t] ON [s].[key]=[t].[key] AND [s].[table]=@erp_tbl WHERE [t].[key] IS NOT NULL
	END
	
	IF EXISTS (SELECT * FROM INSERTED) AND NOT EXISTS (SELECT * FROM DELETED)
	BEGIN
		SET @cmd = 'INSERT'
		INSERT INTO "#trig_tbl" SELECT [key] FROM "#trig_ins"
		DELETE [s] FROM [ceRem3_sync] [s] LEFT JOIN "#trig_tbl" [t] ON [s].[key]=[t].[key] AND [s].[table]=@erp_tbl WHERE [t].[key] IS NOT NULL
	END
	
	IF EXISTS (SELECT * FROM INSERTED) AND EXISTS (SELECT * FROM DELETED)
	BEGIN
		SET @cmd = 'UPDATE'
		INSERT INTO "#trig_tbl" SELECT [key] FROM "#trig_ins"
		DELETE [s] FROM [ceRem3_sync] [s] LEFT JOIN "#trig_tbl" [t] ON [s].[key]=[t].[key] AND [s].[table]=@erp_tbl WHERE [t].[key] IS NOT NULL
	END
	
	INSERT INTO [ceRem3_sync] ([command], [table], [key], [datum])
		SELECT @cmd, @erp_tbl, [t].[key], GETDATE() 
			FROM "#trig_tbl" [t]
			LEFT JOIN [ceRem3_sync] [s] ON [s].[key]=[t].[key] AND [s].[table]=@erp_tbl
			WHERE [s].[key] IS NULL
	
	DROP TABLE #trig_ins
	DROP TABLE #trig_del
	DROP TABLE #trig_tbl
END;





CREATE TRIGGER "ceRem3_CRM_ADRESSEN"
ON "CRM_ADRESSEN" AFTER INSERT, DELETE, UPDATE
AS
BEGIN
	DECLARE 
		@erp_tbl AS VARCHAR(50) = 'CRM_ADRESSEN',
		@cmd AS VARCHAR(50),
		@q AS VARCHAR(MAX)
	
	SELECT [CRM_ADRESSEN_ID] AS [key] INTO "#trig_ins" FROM [INSERTED]
	SELECT [CRM_ADRESSEN_ID] AS [key] INTO "#trig_del" FROM [DELETED]
	
	CREATE TABLE "#trig_tbl" ([key] INT)
	
	IF NOT EXISTS (SELECT * FROM INSERTED) AND EXISTS (SELECT * FROM DELETED)
	BEGIN
		SET @cmd = 'DELETE'
		INSERT INTO "#trig_tbl" SELECT [key] FROM "#trig_del"
		DELETE [s] FROM [ceRem3_sync] [s] LEFT JOIN "#trig_tbl" [t] ON [s].[key]=[t].[key] AND [s].[table]=@erp_tbl WHERE [t].[key] IS NOT NULL
	END
	
	IF EXISTS (SELECT * FROM INSERTED) AND NOT EXISTS (SELECT * FROM DELETED)
	BEGIN
		SET @cmd = 'INSERT'
		INSERT INTO "#trig_tbl" SELECT [key] FROM "#trig_ins"
		DELETE [s] FROM [ceRem3_sync] [s] LEFT JOIN "#trig_tbl" [t] ON [s].[key]=[t].[key] AND [s].[table]=@erp_tbl WHERE [t].[key] IS NOT NULL
	END
	
	IF EXISTS (SELECT * FROM INSERTED) AND EXISTS (SELECT * FROM DELETED)
	BEGIN
		SET @cmd = 'UPDATE'
		INSERT INTO "#trig_tbl" SELECT [key] FROM "#trig_ins"
		DELETE [s] FROM [ceRem3_sync] [s] LEFT JOIN "#trig_tbl" [t] ON [s].[key]=[t].[key] AND [s].[table]=@erp_tbl WHERE [t].[key] IS NOT NULL
	END
	
	INSERT INTO [ceRem3_sync] ([command], [table], [key], [datum])
		SELECT @cmd, @erp_tbl, [t].[key], GETDATE() 
			FROM "#trig_tbl" [t]
			LEFT JOIN [ceRem3_sync] [s] ON [s].[key]=[t].[key] AND [s].[table]=@erp_tbl
			WHERE [s].[key] IS NULL
	
	DROP TABLE #trig_ins
	DROP TABLE #trig_del
	DROP TABLE #trig_tbl
END;






