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に対して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 に答える