
CREATE TRIGGER "ceRem3_BELEGP"
ON "BELEGP" AFTER INSERT, DELETE, UPDATE
AS
BEGIN
--  SET NOCOUNT ON;
  IF EXISTS (SELECT * FROM INSERTED) AND EXISTS (SELECT * FROM DELETED) AND (SELECT b.FreieZahl3 FROM INSERTED AS i LEFT JOIN dbo.BELEG AS b ON b.Belegnummer=i.Belegnummer)=1
    INSERT INTO "ceRem3_sync" ("command", "table", "key", "datum") SELECT 'UPDATE', 'BELEGP', BELEGP_ID, GETDATE() FROM INSERTED
  IF EXISTS (SELECT * FROM INSERTED) AND NOT EXISTS (SELECT * FROM DELETED) AND (SELECT b.FreieZahl3 FROM INSERTED AS i LEFT JOIN dbo.BELEG AS b ON b.Belegnummer=i.Belegnummer)=1
    INSERT INTO "ceRem3_sync" ("command", "table", "key", "datum") SELECT 'INSERT', 'BELEGP', BELEGP_ID, GETDATE() FROM INSERTED
  IF NOT EXISTS (SELECT * FROM INSERTED) AND EXISTS (SELECT * FROM DELETED) AND (SELECT b.FreieZahl3 FROM DELETED AS i LEFT JOIN dbo.BELEG AS b ON b.Belegnummer=i.Belegnummer)=1
    INSERT INTO "ceRem3_sync" ("command", "table", "key", "datum") SELECT 'DELETE', 'BELEGP', BELEGP_ID, GETDATE() FROM DELETED
END
;

CREATE TRIGGER "ceRem3_BELEG"
ON "BELEG" AFTER INSERT, DELETE, UPDATE
AS
BEGIN
--  SET NOCOUNT ON;
  IF EXISTS (SELECT * FROM INSERTED) AND EXISTS (SELECT * FROM DELETED) AND (SELECT FreieZahl3 FROM INSERTED)=1
    INSERT INTO "ceRem3_sync" ("command", "table", "key", "datum") SELECT 'UPDATE', 'BELEG', BELEG_ID, GETDATE() FROM INSERTED
  IF EXISTS (SELECT * FROM INSERTED) AND NOT EXISTS (SELECT * FROM DELETED) AND (SELECT FreieZahl3 FROM INSERTED)=1
    INSERT INTO "ceRem3_sync" ("command", "table", "key", "datum") SELECT 'INSERT', 'BELEG', BELEG_ID, GETDATE() FROM INSERTED
  IF NOT EXISTS (SELECT * FROM INSERTED) AND EXISTS (SELECT * FROM DELETED) AND (SELECT FreieZahl3 FROM DELETED)=1
    INSERT INTO "ceRem3_sync" ("command", "table", "key", "datum") SELECT 'DELETE', 'BELEG', BELEG_ID, GETDATE() FROM DELETED
END
;
