Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8246097
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T22:23:48+00:00 2026-06-07T22:23:48+00:00

I have those tables CREATE TABLE [Test]( [Id] [int] NOT NULL, [Value] [int] NOT

  • 0

I have those tables

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]

On the table Test i have an this update trigger:

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

table test_2 is empty and test have this record

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

when i run this update

UPDATE Test  SET VALUE= 50

i have this kind of error

Msg 2627, Level 14, State 1, Procedure Test_TriggerUpdate, Line 12
Violation PRIMARY KEY ‘PK_Test_2’. Impossible to insert duplicate key
with value (1) into ‘Test_2’.

Maybe this happen when Merge Operation is called with multirows and instead of call before INSERT and next UPDATE operation, it run two INSERT for record 1 and 2.
What is possible to do?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T22:23:49+00:00Added an answer on June 7, 2026 at 10:23 pm

    Easy way to solve this problem is use a instruction of group by on the key and an aggregation function on values to sum as this:

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got the following SQL table: CREATE TABLE [dbo].[Test]( [TestID] [int] NOT NULL, [TestNum]
I have the following table: CREATE TABLE `events` ( `evt_id` int(11) NOT NULL AUTO_INCREMENT,
Strange situation. If I have these tables: CREATE TABLE t1 (id INT, title VARCHAR(20),
I have a table that looks like this: CREATE TABLE [Test].[dbo].[MyTest] ( [Id] INT
I have those two tables: client: id (int) #PK name (varchar) client_category: id (int)
I created two tables: Publisher PK, PublisherId, int, not null, indentity +1 PublisherName, varchar(50),
I Have created a procedure which is: CREATE PROCEDURE test.table_creation ( @ID INT )
In my application I have small tables(files) with unique keys (most of those have
I have two tables, one containing cities and one containing countries. Those are bi-directional
I've 3 different database tables that have the same 5 fields but those does

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.