一尘不染

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

sql

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

更新查询:

 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将如何用于更新查询。


阅读 310

收藏
2021-03-08

共1个答案

一尘不染

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

SELECT会统计所有行,包括那些由加盟倍增,。该UPDATE会仅计算唯一行IM_MarketPlace受影响UPDATE

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

2021-03-08