SQL Server不匹配条件相同的SELECT和UPDATE查询中的行数

时间:2019-06-07 07:44:19

标签: sql sql-server sql-server-2012 sql-server-2016 sql-server-2017

我正在尝试匹配更新并选择条件相同的查询计数。但是行数却相差很大。

更新查询:

 UPDATE  MKP set MKP.Quantity =  MKP.Quantity + LQD.Quantity, ModifiedDate = GETDATE()
         FROM IM_MarketPlace MKP 
              INNER JOIN IM_ChannelListings CL ON MKP.ListingID = CL.ListingID 
              INNER JOIN @ListingQuantityData LQD ON LQD.ChanelListingID = RTRIM(LTRIM((CL.ChannelListingID)))  and LQD.SalesChannelID = CL.ChannelID
              Left outer join IM_ListingVariations LV on LV.ListingCode = RTRIM(LTRIM((LQD.VariationSKU))) and  MKP.ListingVariationID = LV.ID and CL.ListingID=LV.ListingID
         WHERE MKP.IsActive =1 and MKP.IsDeleted=0 and CL.IsActive =1 and CL.IsDeleted=0 
               AND (LQD.VariationSKU is null OR (LQD.VariationSKU = LV.ListingCode and lv.ID = MKP.ListingVariationID))

选择查询

select count(mkp.ListingID) FROM IM_MarketPlace MKP 
              INNER JOIN IM_ChannelListings CL ON MKP.ListingID = CL.ListingID 
              INNER JOIN @ListingQuantityData LQD ON LQD.ChanelListingID = RTRIM(LTRIM((CL.ChannelListingID)))  and LQD.SalesChannelID = CL.ChannelID
              Left outer join IM_ListingVariations LV on LV.ListingCode = RTRIM(LTRIM((LQD.VariationSKU))) and  MKP.ListingVariationID = LV.ID and CL.ListingID=LV.ListingID
         WHERE MKP.IsActive =1 and MKP.IsDeleted=0 and CL.IsActive =1 and CL.IsDeleted=0 
               AND (LQD.VariationSKU is null OR (LQD.VariationSKU = LV.ListingCode and lv.ID = MKP.ListingVariationID))

为此请帮帮我。

,也请让我知道@@ rowcount如何用于更新查询。

1 个答案:

答案 0 :(得分:2)

如果联接中涉及的至少两个表之间存在一对多关系,则会发生这种情况。

SELECT将计算所有行,包括通过联接相乘的行。 UPDATE只会计算受IM_MarketPlace影响的UPDATE中的唯一行。

在存在一对多关系的情况下,不确定IM_MarketPlace中连接到特定行的“许多”行中的哪一行被用作该行的更新中的源。