[步骤]
declare @IsFactoryDirectDeliveryOrder bit=?,@CustomerID int=1UPDATE sp SET sp.IsFactoryDirectDeliveryOrder=@IsFactoryDirectDeliveryOrder
FROM RB_Procurement_List sp(NOLOCK)
INNER JOIN RB_Procurement_Order_PositionCode pop(NOLOCK) ON sp.P_Code=pop.P_Code AND sp.CustomerID=pop.CustomerID
INNER JOIN RB_OrderInfo_BeikuExpand(NOLOCK) ob ON pop.OrderCode=ob.OrderCode AND pop.CustomerID=ob.CustomerID
WHERE ob.RestockType=2 AND sp.CustomerID=@CustomerID AND ISNULL(sp.IsFactoryDirectDeliveryOrder,0)=0;
UPDATE sp SET sp.IsFactoryDirectDeliveryOrder=1,FactoryDirectDeliveryProductNumber=b.sumProductNum
FROM RB_Procurement_Detail sp(NOLOCK)
INNER JOIN (SELECT pop.P_Code,pop.SKU,pop.CustomerID,SUM(pop.ProductNum+ProductNumUpdate) sumProductNum FROM RB_Procurement_Order_PositionCode pop(NOLOCK)
INNER JOIN RB_OrderInfo_BeikuExpand ob(NOLOCK) ON pop.OrderCode=ob.OrderCode AND pop.CustomerID=ob.CustomerID WHERE pop.CustomerID=@CustomerID AND ob.RestockType=2 GROUP BY pop.CustomerID,pop.P_Code,pop.SKU) b on b.CustomerID=sp.CustomerID AND b.SKU=sp.SKU AND b.P_Code=sp.P_Code
WHERE sp.CustomerID=@CustomerID AND ISNULL(sp.IsFactoryDirectDeliveryOrder,0)=0;

[结果]
[期望]
把语句都拆分成 多个 SQL ,一个一个的执行。 先获取 需要更新的主键 数据 , 再通过 此主键去 更新 对应的数据值
工厂直发的逻辑