SQL Server Update with Inner Join

前端 未结 3 976
离开以前
离开以前 2020-12-29 03:42

I have 3 tables (simplified):

 tblOrder(OrderId INT)  
  tblVariety(VarietyId INT,Stock INT)  
  tblOrderItem(OrderId,VarietyId,Quantity INT)
相关标签:
3条回答
  • 2020-12-29 04:23

    You are doing an update. It will update once.

    Edit: to solve, you can add in a subquery that will group your orderitems by orderid and varietyid, with a sum on the amount.

    0 讨论(0)
  • 2020-12-29 04:27

    My guess is that because you have shown us simplified schema, some info is missing that would determine why have the repeated VarietyID values for a given OrderID.

    When you have multiple rows, SQL Server will arbritrarily pick one of them for the update.

    If this is the case, you need to group first

    UPDATE V
    SET
       Stock = Stock - foo.SumQuantity
    FROM
        tblVariety V
        JOIN
        (SELECT SUM(Quantity) AS SumQuantity, VarietyID
         FROM tblOrderItem
          JOIN tblOrder ON tblOrderItem.OrderId = tblOrder.OrderId  
         WHERE tblOrder.OrderId = 1
         GROUP BY VarietyID
        ) foo ON V.VarietyId = foo.VarietyId  
    

    If not, then the OrderItems table PK is wrong because if allows duplicate OrderID/VarietyID combinations (The PK should be OrderID/VarietyID, or these should be constrained unique)

    0 讨论(0)
  • 2020-12-29 04:36

    From the documentation UPDATE

    The results of an UPDATE statement are undefined if the statement includes a FROM clause that is not specified in such a way that only one value is available for each column occurrence that is updated (in other words, if the UPDATE statement is not deterministic). For example, given the UPDATE statement in the following script, both rows in table s meet the qualifications of the FROM clause in the UPDATE statement, but it is undefined which row from s is used to update the row in table t.

    CREATE TABLE s (ColA INT, ColB DECIMAL(10,3))
    GO
    CREATE TABLE t (ColA INT PRIMARY KEY, ColB DECIMAL(10,3))
    GO
    INSERT INTO s VALUES(1, 10.0)
    INSERT INTO s VALUES(1, 20.0)
    INSERT INTO t VALUES(1, 0.0)
    GO
    UPDATE t 
    SET t.ColB = t.ColB + s.ColB
    FROM t INNER JOIN s ON (t.ColA = s.ColA)
    GO
    
    0 讨论(0)
提交回复
热议问题