-2

For the eternity that I have been spending time on this, I can't seem to find the cause as to why my MERGE statement inserts duplicate rows. Here are my tables.

TABLE INVENTORY
ProductID  |   ProductName   |   ProductCode   |   Quantity   |   Location
1          |   Stabilo       |   Code123       |   3          |   Basement
2          |   Parker Pen    |   Code456       |   4          |   Basement

TABLE INCOMINGSTOCKS
REQUESTNUMBER  |  ProductID    | ProductName  | ProductCode  | Quantity  | DeliveryLocation
Request123     |  2            | Parker Pen   | Code456      | 3         | Basement
Request123     |  3            | Eraser       | Code789      | 5         | Basement

One request number = multiple items, much like a fast food delivery can contain multiple orders in one transaction number.

When I run this query...

MERGE INVENTORY as T1
USING INCOMINGSTOCKS AS T2
ON T1.ProductCode = T2.ProductCode
AND T2.REQUESTNUMBER = 'Request123' and T2.DeliveryLocation= 'Basement'
WHEN MATCHED THEN
UPDATE SET T1.Quantity = T1.Quantity + T2.Quantity
WHEN NOT MATCHED THEN
INSERT (ProductID, ProductName, ProductCode, Quantity, Location) 
VALUES (T2.ProductID, T2.ProductName, T2.ProductCode, T2.Quantity, T2.DeliveryLocation);


...it returns with this data:

ProductID  | ProductName  |  ProductCode  |  Quantity  |  Location
Stabilo    | 1            |  Code123      |  3         |  Basement
Stabilo    | 1            |  Code123      |  3         |  Basement
Parker Pen | 2            |  Code456      |  7         |  Basement
Parker Pen | 2            |  Code456      |  4         |  Basement

The "Eraser" item did not even get inserted! It only duplicated Stabilo (which was not in the INCOMINGSTOCKS table, added the quantity of Parker Pens (3+4) and reinserted it again this time with its initial quantity.

Please, can someone help me? Any insight or any comment regarding my query? Is there something wrong with it?

THANK YOU!!!

James
  • 729
  • 6
  • 10
Saudate
  • 431
  • 2
  • 8
  • 14
  • Possibly inculde a check in yoir WHERE clause for: ISNULL (T1.ProductCode,'') != '' – Daniel Nov 13 '16 at 04:50
  • Your posted `merge` statement isn't even valid. Can you post the one you actually ran? – sstan Nov 13 '16 at 05:16
  • @sstan I have corrected the merge statement, thank you. – Saudate Nov 13 '16 at 05:46
  • 3
    I still don't think you actually ran that statement successfully. I get an `Incorrect syntax near the keyword 'WHERE'.` error. – sstan Nov 13 '16 at 05:50
  • Sorry, replaced `where` with `and` – Saudate Nov 13 '16 at 06:01
  • god!, then it will work – James Nov 13 '16 at 06:04
  • Can't repro your problem: http://rextester.com/DAEB85559. You are clearly not stating everything. You already gave us the wrong statement a few times, it's hard to trust the rest of the question. – sstan Nov 13 '16 at 06:14
  • I actually asked a question of the same nature twice here at SO but only got a few replies. So I tried to change my query to a simpler one. Here is a link to one of the questions I submitted (but not using Merge). [Link](http://stackoverflow.com/questions/40509470/insert-data-if-not-exists-from-2-tables-and-update-otherwise) – Saudate Nov 13 '16 at 06:24
  • above query wont return the data as you have mentioned. – James Nov 13 '16 at 08:09

1 Answers1

1

I kinda didnt understand T2.DestinationLocation, T2.Location, USING INCOMING STOCKS AS T2

Anyways try like this:

MERGE INVENTORY as T1
USING INCOMINGSTOCKS AS T2
ON T1.ProductCode = T2.ProductCode
and T2.REQUESTNUMBER = 'Request123' and T2.DeliveryLocation = 'Basement'
WHEN MATCHED THEN
UPDATE SET T1.Quantity = T1.Quantity + T2.Quantity
WHEN NOT MATCHED THEN
INSERT (ProductID, ProductName, ProductCode, Quantity, Location) 
VALUES (T2.ProductID, T2.ProductName, T2.ProductCode, T2.Quantity, T2.DeliveryLocation);

select * from INVENTORY
James
  • 729
  • 6
  • 10
  • Sorry. I have corrected the query. `T2.DestinationLocation and T2.Location are T2. DeliveryLocation.` Removed the space in `INCOMING STOCKS` to `INCOMINGSTOCKS AS T2` – Saudate Nov 13 '16 at 05:45