1

我有那些桌子

CREATE TABLE [Test](
    [Id] [int] NOT NULL,
    [Value] [int] NOT NULL,
    [Id_Test_2] [int] NOT NULL,
 CONSTRAINT [PK_Test] PRIMARY KEY CLUSTERED 
(
    [Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]



CREATE TABLE [Test_2](
    [Id_Test_2] [int] NOT NULL,
    [Value_Test_2] [int] NOT NULL,
 CONSTRAINT [PK_Test_2] PRIMARY KEY CLUSTERED 
(
    [Id_Test_2] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

在测试表上,我有一个更新触发器:

CREATE TRIGGER [dbo].[Test_TriggerUpdate]
ON [dbo].[Test]

FOR UPDATE 

NOT FOR REPLICATION

AS
BEGIN

MERGE Test_2 AS Target

    USING 
        ( SELECT    D.Id_Test_2 ,
                    ( COALESCE(D.Value, 0) * 2 ) AS Value 
          FROM      Deleted D                    
        ) AS Source
    ON ( Target.Id_Test_2 = Source.Id_Test_2       )
    WHEN MATCHED 
        THEN 
            UPDATE
          SET               TARGET.Value_Test_2 = ( TARGET.Value_Test_2 - Source.Value )
    WHEN NOT MATCHED BY TARGET 
        THEN
        INSERT  (
                      Id_Test_2 ,
                      Value_Test_2

                    )
          VALUES    ( Source.Id_Test_2 ,                      
                      (Source.[Value]*(-1))
                    );



  MERGE Test_2 AS Target
    USING 
        ( SELECT    I.Id_Test_2 ,
                    ( COALESCE(I.Value, 0)
                      * 2 ) AS Value
          FROM      INSERTED I
        ) AS Source
    ON ( Target.Id_Test_2 = Source.Id_Test_2
       )
    WHEN MATCHED 
        THEN 
            UPDATE
          SET               TARGET.Value_Test_2 = ( TARGET.Value_Test_2    + Source.Value )
    WHEN NOT MATCHED BY TARGET 
        THEN
        INSERT  (
                      Id_Test_2 ,
                      Value_Test_2

                    )
          VALUES    ( Source.Id_Test_2 ,
                      Source.[Value]
                    );

END

表 test_2 为空,测试有此记录

Id  Value   Id_Test_2
1   10  1
2   20  1
3   30  2

当我运行此更新时

UPDATE Test  SET VALUE= 50

我有这种错误

消息 2627,级别 14,状态 1,过程 Test_TriggerUpdate,第 12 行违反主键“PK_Test_2”。无法将值为 (1) 的重复键插入“Test_2”。

当使用多行调用合并操作时可能会发生这种情况,而不是在 INSERT 和下一个 UPDATE 操作之前调用,它为记录 1 和 2 运行两个 INSERT。可以做什么?

4

1 回答 1

1

解决这个问题的简单方法是在键上使用 group by 指令和在值上使用聚合函数来求和,如下所示:

ALTER TRIGGER [dbo].[Test_TriggerUpdate]
ON [dbo].[Test]

FOR UPDATE 

NOT FOR REPLICATION

AS
BEGIN

  MERGE Test_2 AS Target
    USING 
        ( SELECT    D.Id_Test_2 ,
                    sum(( COALESCE(D.Value, 0) * 2 )) AS Value
          FROM      Deleted D      
          GROUP BY  Id_Test_2
        ) AS Source
    ON ( Target.Id_Test_2 = Source.Id_Test_2 )
    WHEN MATCHED 
        THEN 
        UPDATE
          SET TARGET.Value_Test_2 = ( TARGET.Value_Test_2 - Source.Value )
    WHEN NOT MATCHED BY TARGET 
        THEN
        INSERT  ( Id_Test_2 ,
                  Value_Test_2
                )
        VALUES  ( Source.Id_Test_2 ,
                  (Source.[Value]*(-1))
                );

  MERGE Test_2 AS Target
    USING 
        ( SELECT    I.Id_Test_2 ,
                    sum(( COALESCE(I.Value, 0) * 2 )) AS Value
          FROM      INSERTED I
          GROUP BY  Id_Test_2  
        ) AS Source
    ON ( Target.Id_Test_2 = Source.Id_Test_2 )
    WHEN MATCHED 
        THEN 
        UPDATE
          SET TARGET.Value_Test_2 = ( TARGET.Value_Test_2 + Source.Value )
    WHEN NOT MATCHED BY TARGET 
        THEN
        INSERT  ( Id_Test_2 ,
                  Value_Test_2
                )
        VALUES  ( Source.Id_Test_2 ,
                  Source.[Value]
                );

END
于 2012-07-19T11:36:20.267 回答