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

  • SEARCH
  • Home
  • 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 7013921
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:27:12+00:00 2026-05-27T22:27:12+00:00

I have two tables TableA and TableB as follows: TableA: ItemID Qty Rate ——–

  • 0

I have two tables TableA and TableB as follows:

TableA:

     ItemID          Qty           Rate
    --------        -----         --------
       1             10            100.00
       2             20            150.00

TableB:

     ItemID          Qty           Rate
    --------        -----         -------
       1              5            150.00
       3              10           200.00
       3              20           400.00

Now I want to consolidate these two tables. My desired result needs to be as follows:

Result TableA:

     ItemID           Qty           Rate
    --------         -----         -------
       1              15            150.00
       2              20            150.00
       3              30            400.00

I tried the following Insert Select statement, But it does not give the desired result.

INSERT INTO TableA
(
     ItemID,
     Qty,
     Rate               
)
SELECT 
    ItemID, 
    SUM(Qty), 
    MAX(Rate)
FROM 
    TableB
GROUP BY 
    ItemID

But it gives the result as follows:

     ItemID          Qty           Rate
    --------        -----         --------
       1             10            100.00
       2             20            150.00
       1              5            150.00
       3              30           400.00

How to achieve my desired result?

I tried like this:

 MERGE PUR_PODetail AS Target
                USING (
                    SELECT 
                        @POID,
                        ItemID,
                        SUM(POQuantity),
                        MAX(UnitRate),
                        1,
                        CASE WHEN D1 = '' THEN NULL ELSE D1 END D1,
                        CASE WHEN D2 = '' THEN NULL ELSE D2 END D2,
                        CASE WHEN D3 = '' THEN NULL ELSE D3 END D3,
                        CASE WHEN RandomDimension = '' THEN NULL ELSE RandomDimension END RandomDimension,
                        0
                    FROM 
                        @Detail
                    GROUP BY 
                        ItemID, D1, D2, D3, RandomDimension
                    ) AS Source  ON (Target.ItemID = Source.ItemID) AND 
                    (ISNULL(Target.D1, -999) = ISNULL(Source.D1, -999)) AND 
                    (ISNULL(Target.D2, -999) = ISNULL(Source.D2, -999)) AND 
                    (ISNULL(Target.D3, -999) = ISNULL(Source.D3, -999)) AND
                    (ISNULL(Target.RandomDimension, -999) = ISNULL(Source.RandomDimension, -999))
                WHEN MATCHED
                    THEN UPDATE SET 
                            Target.POQuantity = Target.POQuantity + Source.POQuantity,
                            Target.UnitRate = MAX(Source.UnitRate)
                WHEN NOT MATCHED
                    INSERT
                        (
                            POID,
                            ItemID,
                            POQuantity,
                            UnitRate,
                            ItemStatusID,
                            D1,
                            D2,
                            D3,
                            RandomDimension,
                            EDInclusive_f
                        )
                    VALUES
                        (
                            @POID, 
                            Source.ItemID, 
                            Source.POQuantity, 
                            Source.UnitRate, 
                            1,
                            CASE WHEN Source.D1 = '' THEN NULL ELSE Source.D1 END D1,
                            CASE WHEN Source.D2 = '' THEN NULL ELSE Source.D2 END D2,
                            CASE WHEN Source.D3 = '' THEN NULL ELSE Source.D3 END D3,
                            CASE WHEN Source.RandomDimension = '' THEN NULL ELSE Source.RandomDimension END RandomDimension,
                            0
                        )

But it gives the following error.
Please correct the error. I dont know what would be wrong here.

Msg 102, Level 15, State 1, Procedure PUR_PurchaseOrder_IU, Line 936
Incorrect syntax near ‘MERGE’.
Msg 156, Level 15, State 1, Procedure PUR_PurchaseOrder_IU, Line 953
Incorrect syntax near the keyword ‘AS’.

But when I remove these merge statement from my stored procedure, it is executing…

  • 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-05-27T22:27:13+00:00Added an answer on May 27, 2026 at 10:27 pm

    You can’t just use the INSERT statement for this, you have to either INSERTor UPDATEdepending on the ItemID already being present in your target table.

    SQL Server 2005

    UPDATE  @TableA
    SET     Qty = a.Qty + b.Qty
            , Rate = CASE WHEN a.Rate < b.Rate
                          THEN b.Rate 
                          ELSE a.Rate 
                      END
    FROM    @TableA a
            INNER JOIN (
              SELECT  ItemID
                      , Qty = SUM(Qty)
                      , Rate = MAX(Rate)
              FROM    @TableB
              GROUP BY 
                      ItemID  
            ) b ON a.ItemID = b.ItemID
    
    INSERT INTO @TableA
    SELECT  ItemID, Qty, Rate
    FROM    ( SELECT  ItemID
                      , Qty = SUM(Qty)
                      , Rate = MAX(Rate)
              FROM    @TableB b
              WHERE   NOT EXISTS (SELECT * FROM @TableA a WHERE a.ItemID = b.ItemID)
              GROUP BY 
                      ItemID  
            ) b
    

    SQL Server 2008 provides the MERGE statement for this.

    Performs insert, update, or delete operations on a target table based on the results of a join with a source table. For example, you
    can synchronize two tables by inserting, updating, or deleting rows in
    one table based on differences found in the other table.

    SQL Server 2008

    MERGE @TableA AS Target
    USING (
      SELECT  ItemID
              , Qty = SUM(Qty)
              , Rate = MAX(Rate)
      FROM    @TableB
      GROUP BY 
              ItemID  
    ) AS source (ItemID, Qty, Rate) ON (target.ItemID = source.ItemID)
    WHEN MATCHED THEN 
      UPDATE SET target.Qty = target.Qty + source.Qty
                 , target.Rate = CASE WHEN target.Rate < source.Rate 
                                      THEN source.Rate 
                                      ELSE target.Rate 
                                 END
    WHEN NOT MATCHED THEN
      INSERT (ItemID, Qty, Rate)
      VALUES (source.ItemID, source.Qty, source.Rate);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say for example, I have the following two tables: TableA { _id } TableB
Scenario: Let's say I have two tables, TableA and TableB. TableB's primary key is
I have two tables, tableA and tableB. I want to set a trigger. Once
I have two tables as follows: tblCountry (countryID, countryCode) tblProjectCountry(ProjectID, countryID) The tblCountry table
To illustrate, assume that I have two tables as follows: VehicleID Name 1 Chuck
If I have two tables - Logins and Users, as follows: Logins LoginIdNo UserIdNo
i have two mysql tables tableA colA1 colA2 1 whatever 2 whatever 3 whatever
i have two msaccess tables tableA textA numA bd 1 as 0 aa 2
I have two tables, related by a common key. So TableA has key AID
I have two tables as follows: tbl_answers : id, (FK)authorID ... tbl_answer_votes : id,

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.