表A上的触发器合并(Upsert)到表B中

时间:2017-05-26 01:49:09

标签: tsql merge triggers sql-server-2008-r2

我正在尝试向表A添加一个触发器,只要执行插入或更新,它就会在表B中执行upsert。我正在使用MERGE来实现这一目标。

此数据库在SQL Server 2008 R2上运行。

这是我的剧本:

USE [XMPie]
GO

/****** Object:  Trigger [dbo].[tr_targetupd40836-09]    Script Date: 5/25/2017 10:18:30 AM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


CREATE TRIGGER [dbo].[tr_targetupd40872-01a]
ON [dbo].[40872-01]
after update, insert as

DECLARE @CJ int, @BPID int, @Email varchar(100)

IF (SELECT TOP 1 OptOut FROM inserted) = 1
BEGIN
    SELECT top 1 @CJ = CAST([MicrositeNumber] AS int) FROM inserted

    SELECT TOP 1 @BPID =  bp.BPID
            FROM JandLreporting.dbo.ControlJob cj
            INNER JOIN JandLreporting.dbo.BusinessPartner bp
            ON cj.JLMReferenceID = bp.JLMReferenceID
            WHERE cj.ControlJob = @CJ

    SELECT top 1 @Email = RTRIM(LTRIM(Email)) FROM inserted

    IF @BPID IS NOT NULL AND NOT EXISTS (SELECT * FROM [DoNotContactEmail] where Email = @Email and BPID = @BPID)
    INSERT [DoNotContactEmail](email, bpid) VALUES(@Email, @BPID)
END
ELSE
BEGIN
    MERGE JandLreporting.dbo.MicrositeResponseAdor AS T  
    USING inserted AS S 
    ON T.RecipientKey = S.RecipientKey
    WHEN MATCHED THEN 
         UPDATE set T.[MicrositeResponse_ID]= (s.[MicrositeResponse_ID])
        ,T.[MicrositeNumber]=LEFT(s.[MicrositeNumber],50)
        ,T.[RecipientKey]=LEFT(s.[RecipientKey],101)
        ,T.[Vin]=LEFT(s.[Vin],30)
        ,T.[FirstName]=LEFT(s.[FirstName],50)
        ,T.[MiddleInitial]=LEFT(s.[MiddleInitial],1)
        ,T.[LastName]=LEFT(s.[LastName],50)
        ,T.[FullName]=LEFT(s.[FullName],100)
        ,T.[Address1]=LEFT(s.[Address1],100)
        ,T.[Address2]=LEFT(s.[Address2],50)
        ,T.[City]=LEFT(s.[City],50)
        ,T.[State]=LEFT(s.[State],2)
        ,T.[ZipCode]=LEFT(s.[ZipCode],10)
        ,T.[PhoneNumber]=LEFT(s.[PhoneNumber],15)
    WHEN NOT MATCHED THEN  
      INSERT (T.[ID]
        ,T.[MicrositeResponse_ID]
        ,T.[MicrositeNumber]
        ,T.[RecipientKey]
        ,T.[Vin]
        ,T.[FirstName]
        ,T.[MiddleInitial]
        ,T.[LastName]
        ,T.[FullName]
        ,T.[Address1]
        ,T.[Address2]
        ,T.[City]
        ,T.[State]
        ,T.[ZipCode]
        ,T.[PhoneNumber]
        )
      VALUES (s.[ID]
        ,s.[MicrositeResponse_ID]
        ,s.[MicrositeNumber]
        ,s.[RecipientKey]
        ,s.[Vin]
        ,s.[FirstName]
        ,s.[MiddleInitial]
        ,s.[LastName]
        ,s.[FullName]
        ,s.[Address1]
        ,s.[Address2]
        ,s.[City]
        ,s.[State]
        ,s.[ZipCode]
        ,s.[PhoneNumber])
END

GO

当我尝试应用此触发器时,出现以下错误:

Msg 102, Level 15, State 1, Procedure tr_targetupd40872-01a, Line 26 [Batch Start Line 9]
Incorrect syntax near ' '.

我无法弄清楚语法错误的位置。任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:0)

您需要在BEGIN / END中包含触发器的代码,如下所示:

CREATE TRIGGER [dbo].[tr_targetupd40872-01a]
ON [dbo].[40872-01]
after update, insert as
BEGIN --begin trigger
  DECLARE @CJ int, @BPID int, @Email varchar(100)

  IF (SELECT TOP 1 OptOut FROM inserted) = 1
  BEGIN
    SELECT top 1 @CJ = CAST([MicrositeNumber] AS int) FROM inserted

    SELECT TOP 1 @BPID =  bp.BPID
      FROM JandLreporting.dbo.ControlJob cj
        INNER JOIN JandLreporting.dbo.BusinessPartner bp
          ON cj.JLMReferenceID = bp.JLMReferenceID
      WHERE cj.ControlJob = @CJ

    SELECT top 1 @Email = RTRIM(LTRIM(Email)) FROM inserted

    IF @BPID IS NOT NULL AND NOT EXISTS (SELECT * FROM [DoNotContactEmail] where Email = @Email and BPID = @BPID)
    INSERT [DoNotContactEmail](email, bpid) VALUES(@Email, @BPID)
  END
  ELSE
  BEGIN
    MERGE JandLreporting.dbo.MicrositeResponseAdor AS T  
      USING inserted AS S 
      ON T.RecipientKey = S.RecipientKey
      WHEN MATCHED THEN UPDATE
        set T.[MicrositeResponse_ID]= (s.[MicrositeResponse_ID])
           ,T.[MicrositeNumber]=LEFT(s.[MicrositeNumber],50)
           ,T.[RecipientKey]=LEFT(s.[RecipientKey],101)
           ,T.[Vin]=LEFT(s.[Vin],30)
           ,T.[FirstName]=LEFT(s.[FirstName],50)
           ,T.[MiddleInitial]=LEFT(s.[MiddleInitial],1)
           ,T.[LastName]=LEFT(s.[LastName],50)
           ,T.[FullName]=LEFT(s.[FullName],100)
           ,T.[Address1]=LEFT(s.[Address1],100)
           ,T.[Address2]=LEFT(s.[Address2],50)
           ,T.[City]=LEFT(s.[City],50)
           ,T.[State]=LEFT(s.[State],2)
           ,T.[ZipCode]=LEFT(s.[ZipCode],10)
           ,T.[PhoneNumber]=LEFT(s.[PhoneNumber],15)
      WHEN NOT MATCHED THEN INSERT (
            T.[ID]
           ,T.[MicrositeResponse_ID]
           ,T.[MicrositeNumber]
           ,T.[RecipientKey]
           ,T.[Vin]
           ,T.[FirstName]
           ,T.[MiddleInitial]
           ,T.[LastName]
           ,T.[FullName]
           ,T.[Address1]
           ,T.[Address2]
           ,T.[City]
           ,T.[State]
           ,T.[ZipCode]
           ,T.[PhoneNumber]
        ) VALUES (
           s.[ID]
          ,s.[MicrositeResponse_ID]
          ,s.[MicrositeNumber]
          ,s.[RecipientKey]
          ,s.[Vin]
          ,s.[FirstName]
          ,s.[MiddleInitial]
          ,s.[LastName]
          ,s.[FullName]
          ,s.[Address1]
          ,s.[Address2]
          ,s.[City]
          ,s.[State]
          ,s.[ZipCode]
          ,s.[PhoneNumber]
        )
  END
END  -- End of trigger
GO