UPDATE @DList1
SET
Listed = a.Listed + b.Listed,
Sold = b.Sold,
Volume = a.Volume + ISNULL(b.Volume, 0)
FROM @DList1 a, @DList2 b
WHERE a.ItemID = b.ItemID
在上面更新的语句中,列[Listed]
没有正确更新[Volume]
。
列表2
ItemID Listed Sold Volume
104 NULL 1 266000
778 1 1 5390
200 1 0 266000
列表1
ItemID Listed Sold Volume
200 1 0 6395000
779 1 0 155000
更新后@Dlist1
ItemID Listed Sold Volume
200 1 0 6661000
779 1 0 155000
方案:
DECLARE @DList1 TABLE
(
ItemID NVARCHAR(20)
, Listed BIGINT
, Sold BIGINT
, Volume BIGINT
)
DECLARE @DList2 TABLE
(
ItemID NVARCHAR(20)
, Listed BIGINT
, Sold BIGINT
, Volume BIGINT
)