INSERT触发器导致问题

时间:2015-07-03 14:32:49

标签: sql sql-server triggers insert dml

我是编写SQL Server触发器的新手。我有一个名为USERS的表,我还有另一个名为USERS_DELTA的表。这两者之间的区别是USERS_DELTA还有一个名为change_type的列。

以下是表架构:

USERS表:

CREATE TABLE [dbo].[TDR_Users]
(
    [objectGUID] [varbinary](50) NOT NULL,
    [distinguishedName] [nvarchar](255) NOT NULL,
    [adForest] [nvarchar](50) NULL,
    [adDomain] [nvarchar](50) NULL,
    [accountExpires] [datetime] NULL,
    [adminCount] [int] NULL,
    [cn] [nvarchar](64) NULL,
    [company] [nvarchar](64) NULL,
    [description] [nvarchar](448) NULL,
    [displayName] [nvarchar](256) NULL,
    [division] [nvarchar](256) NULL,
    [employeeID] [nvarchar](16) NULL
)

USERS_DELTA表:

CREATE TABLE [dbo].[TDR_Users]
(
    [objectGUID] [varbinary](50) NOT NULL,
    [distinguishedName] [nvarchar](255) NOT NULL,
    [adForest] [nvarchar](50) NULL,
    [adDomain] [nvarchar](50) NULL,
    [accountExpires] [datetime] NULL,
    [adminCount] [int] NULL,
    [cn] [nvarchar](64) NULL,
    [company] [nvarchar](64) NULL,
    [description] [nvarchar](448) NULL,
    [displayName] [nvarchar](256) NULL,
    [division] [nvarchar](256) NULL,
    [employeeID] [nvarchar](16) NULL,
    [change_Type] [nvarchar](10) NULL
)

我有一个应用程序,它将在USERS表中创建记录。但我要做的是将插入捕获到USERS_DELTA。我在USERS表上写了一个触发器:

CREATE TRIGGER [dbo].[TR_INSERTS_DELTAS]
   ON  [dbo].[Users]
   FOR INSERT
AS 
    DECLARE @ObjectGUID varbinary(50), @DN varchar(255), @memcount int;
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    -- Get the primary and unique keys from the inserted rows.
    SELECT @DN=i.distinguishedName  FROM inserted i;
    SELECT @ObjectGUID = i.objectGUID FROM inserted i;  

    -- Check if a row already exists in the TDR_Users_Delta table with those values.
    SELECT @memcount=COUNT(*) FROM Users 
    WHERE Users.distinguishedName = @DN 
    AND Users.objectGUID = @ObjectGUID ;
    if(@memcount = 0)
    BEGIN
        INSERT INTO [dbo].[Users_Delta]
        (
            [objectGUID],
            [distinguishedName],
            [adForest],
            [adDomain],
            [accountExpires],
            [adminCount],
            [cn] ,
            [company],
            [description],
            [displayName],
            [division],
            [employeeID],
            [change_type]
        )

        VALUES
        (
            INSERTED.[objectGUID],
            INSERTED.[distinguishedName],
            INSERTED.[adForest],
            INSERTED.[adDomain],
            INSERTED.[accountExpires],
            INSERTED.[adminCount],
            INSERTED.[cn] ,
            INSERTED.[company],
            INSERTED.[description],
            INSERTED.[displayName],
            INSERTED.[division],
            INSERTED.[employeeID],
            'Add'           
        );

    END
END
GO

当我执行此触发器时,出现以下错误:

  

Msg 4104,Level 16,State 1,Procedure TR_INSERTS_DELTAS,Line 94
  无法绑定多部分标识符“Inserted.objectGUID”。

     

Msg 4104,Level 16,State 1,Procedure TR_INSERTS_DELTAS,Line 95
  无法绑定多部分标识符“INSERTED.distinguishedName”。

     

Msg 4104,Level 16,State 1,Procedure TR_INSERTS_DELTAS,Line 96
  无法绑定多部分标识符“INSERTED.adForest”。

     

Msg 4104,Level 16,State 1,Procedure TR_INSERTS_DELTAS,Line 97
  无法绑定多部分标识符“INSERTED.adDomain”。

     

Msg 4104,Level 16,State 1,Procedure TR_INSERTS_DELTAS,Line 98
  ...

我做错了什么? :(

1 个答案:

答案 0 :(得分:1)

我认为您只需要使用表格中的select而不是使用inserted.x来表示插入。

CREATE TRIGGER [dbo].[TR_INSERTS_DELTAS]
   ON  [dbo].[Users]
   FOR INSERT
AS 
DECLARE @ObjectGUID varbinary(50), @DN varchar(255), @memcount int;
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.

    SET NOCOUNT ON;

    -- Get the primary and unique keys from the inserted rows.
    SELECT @DN=i.distinguishedName  FROM inserted i;
    SELECT @ObjectGUID = i.objectGUID FROM inserted i;  

    -- Check if a row already exists in the TDR_Users_Delta table with those values.
    SELECT @memcount=COUNT(*) FROM Users 
    WHERE Users.distinguishedName = @DN 
    AND Users.objectGUID = @ObjectGUID ;
    if(@memcount = 0)
    BEGIN
        INSERT INTO [dbo].[Users_Delta]
        (
            [objectGUID],
            [distinguishedName],
            [adForest],
            [adDomain],
            [accountExpires],
            [adminCount],
            [cn] ,
            [company],
            [description],
            [displayName],
            [division],
            [employeeID],
            [change_type]
        )

        select
            INSERTED.[objectGUID],
            INSERTED.[distinguishedName],
            INSERTED.[adForest],
            INSERTED.[adDomain],
            INSERTED.[accountExpires],
            INSERTED.[adminCount],
            INSERTED.[cn] ,
            INSERTED.[company],
            INSERTED.[description],
            INSERTED.[displayName],
            INSERTED.[division],
            INSERTED.[employeeID],
            'Add'
From inserted
    END
END
GO