declare @PODetail table ( PODetailID int, StatusID int )...

0

Please login or register to vote for this query.

(click on this box to dismiss)

Stack Overflow

Q&A for programmers

-- Enter Query Title
-- Enter Query Description

declare @PODetail table
(
  PODetailID int,
  StatusID int
)

declare @MRVDetail table
(
  AcceptedQty int,
  PODetailID int
)

declare @Detail table
(
  PODetailID int,
  POQuantity int
)
  
insert into @PODetail values (1, 2),(2, 2),(3, 2)
insert into @MRVDetail values (3, 3),(4, 3),(10, 4)
insert into @Detail values (3, 7)


UPDATE @PODetail
SET    StatusID = 4
WHERE  PODetailID IN (SELECT d.PODetailID
                      FROM   @Detail AS d
                      INNER JOIN (SELECT PODetailID,
                                         SUM(AcceptedQty) AS Qty
                                  FROM   @MRVDetail
                                  GROUP BY PODetailID) AS m
                      ON d.PODetailID = m.PODetailID AND 
                         d.POQuantity = m.Qty)  

select *
from @PODetail  

Enter Parameters

Options:
Switch sites:
loading Hold tight while we fetch your results
:records returned in :time ms:cached