SQL trigger insert merge update

Ved opdatering af tabel1 skal tabel2 opdateres

Der er andre der har lavet før mig. Et eksempel er fra SQL Blog

MERGE INTO Customers AS c
USING      Moved     AS m
      ON   m.SSN      = c.SSN
WHEN MATCHED
THEN UPDATE
     SET   Street  = m.Street,
           HouseNo = m.HouseNo,
           City    = m.City;

SQL trigger on update

-- =============================================
-- Object : Trigger [dbo].[fcs_itemUpdate]
-- Script Date: 08-10-2016 12:31:10
-- =============================================
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author : Frede H.
-- Create date: 2016-10-08
-- Description: Trigger when inventable updates
-- =============================================
CREATE TRIGGER [dbo].[fcs_itemUpdate]
  ON [dbo].[INVENTABLE]
  AFTER UPDATE
AS
BEGIN
  -- SET NOCOUNT ON added to prevent extra result sets from
  -- interfering with SELECT statements.
  SET NOCOUNT ON;
  -- Update statements for trigger here
  merge into fcs_catalog as c
        using INVENTABLE as i
        on i.ITEMNUMBER = c.itemnumber
  when matched
  then update
      set isBlocked = i.BLOCKED,
          itemGroup = i.GROUP_,
          itemName1 = i.ITEMNAME1,
          itemName2 = i.ITEMNAME2,
          itemName3 = i.ITEMNAME3,
          picture   = i.IMAGEFILE;
END
GO

Ved indsæt i tabel1 kopier til tabel2

SQL trigger on insert

-- =============================================
-- Object : Trigger [dbo].[fcs_itemInsert]
-- Script Date: 07-10-2016 20:16:10
-- =============================================
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author : Frede H.
-- Create date: 2016-10-07
-- Description: Copy item to fcs_catalog
-- =============================================
CREATE TRIGGER [dbo].[fcs_itemInsert]
  ON [dbo].[INVENTABLE]
  AFTER INSERT
AS
BEGIN
  -- SET NOCOUNT ON added to prevent extra result sets from
  -- interfering with SELECT statements.
  SET NOCOUNT ON;
  -- Insert statements for trigger here
  insert into dbo.fcs_catalog(
         isBlocked,
         isOnline,
         itemGroup,
         itemName1,
         itemName2,
         itemName3,
         itemNumber,
         lastModified,
         picture)
  select BLOCKED,
         0,
         GROUP_,
         ITEMNAME1,
         ITEMNAME2,
         ITEMNAME3,
         ITEMNUMBER,
         LASTCHANGED,
         IMAGEFILE
  from inserted
END
GO