合并和触发 - 主键违规

时间:2012-07-19 11:00:59

标签: sql-server tsql triggers merge-replication

我有那些表

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

我有这种错误

  

Msg 2627,Level 14,State 1,Procedure Test_TriggerUpdate,Line 12   违反PRIMARY KEY'PK_Test_2'。无法插入重复的密钥   将值(1)改为“Test_2”。

当使用multirows调用Merge Operation而不是在INSERT和next UPDATE操作之前调用时,可能会发生这种情况,它会为记录1和2运行两个INSERT。 有什么可以做的?

1 个答案:

答案 0 :(得分: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