删除提示表条件导致Lineqty不正确

时间:2018-10-21 17:44:36

标签: c# sql stored-procedures

大家好,我有这个存储过程,它使用的是计数表,但是当我删除代码并尝试创建计算列以实现相同效果时,它弄乱了程序,如您在此处看到的qty列中有1它应该做的就是它从理货表中获取该数量,但是当我在下面列出的第二个存储过程中删除了该代码时。

我看到的主要问题是,当前的qty行显示为零。

SELECT        pp.ProductionPlanID, pp.WeekStartDate, pp.WeekEndDate, ppi.ProductionPlanItemID, ppi.SOPOrderReturnLineID, bat.ProductionPlanBatchID, sop.DocumentNo, cust.CustomerAccountName, 
                         bat.Name AS BatchName, si.ItemID, si.Code AS StockCode, si.Name AS StockName, sopLine.AllocatedQuantity, o.Quantity AS LineQty
FROM            FUEL_ProductionPlan pp LEFT JOIN
                         FUEL_ProductionPlanItem ppi ON pp.ProductionPlanID = ppi.ProductionPlanID LEFT JOIN
                         FUEL_ProductionPlanBatch ppb ON ppi.ProductionPlanBatchID = ppb.ProductionPlanBatchID LEFT JOIN
                         SOPOrderReturnLine sopLine ON ppi.SOPOrderReturnLineID = sopLine.SOPOrderReturnLineID LEFT JOIN
                         SOPOrderReturn sop ON sopLine.SOPOrderReturnID = sop.SOPOrderReturnID LEFT JOIN
                         SLCustomerAccount cust ON sop.CustomerID = cust.SLCustomerAccountID LEFT JOIN
                         StockItem si ON sopLine.ItemCode = si.Code LEFT JOIN
                         FUEL_ProductionPlanBatch bat ON ppi.ProductionPlanBatchID = bat.ProductionPlanBatchID LEFT JOIN
                         FUEL_Boxed boxed ON sopLine.SOPOrderReturnLineID = boxed.SopItemLineID AND pp.ProductionPlanID = boxed.ProductionPlanID CROSS APPLY
                             (SELECT        1
                               FROM            master..spt_values v
                               WHERE        v.type = 'P' AND v.number < (sopLine.AllocatedQuantity - ISNULL(boxed.QtyBoxed, 0))) o(Quantity)
WHERE        sopLine.AllocatedQuantity > 0

这是导致此问题的代码。

(SELECT        1
FROM            master..spt_values v
WHERE        v.type = 'P' AND v.number < (sopLine.AllocatedQuantity - > 
  ISNULL(boxed.QtyBoxed, 0)))
 o(Quantity)

enter image description here

如您所见,这里正确获取了行数。

这是我修改后的查询,我认为它与拆分表的功能相同,但不一样,并且会引起问题。

SELECT        pp.ProductionPlanID, pp.WeekStartDate, pp.WeekEndDate, ppi.ProductionPlanItemID, ppi.SOPOrderReturnLineID, bat.ProductionPlanBatchID, sop.DocumentNo, 
                         cust.CustomerAccountName, bat.Name AS BatchName, si.ItemID, si.Code AS StockCode, si.Name AS StockName, sopLine.AllocatedQuantity, boxed.QtyBoxed, 
                         ISNULL(sopLine.AllocatedQuantity - boxed.QtyBoxed, 0) AS LineQty
FROM            dbo.FUEL_ProductionPlan AS pp LEFT OUTER JOIN
                         dbo.FUEL_ProductionPlanItem AS ppi ON pp.ProductionPlanID = ppi.ProductionPlanID LEFT OUTER JOIN
                         dbo.FUEL_ProductionPlanBatch AS ppb ON ppi.ProductionPlanBatchID = ppb.ProductionPlanBatchID LEFT OUTER JOIN
                         dbo.SOPOrderReturnLine AS sopLine ON ppi.SOPOrderReturnLineID = sopLine.SOPOrderReturnLineID LEFT OUTER JOIN
                         dbo.SOPOrderReturn AS sop ON sopLine.SOPOrderReturnID = sop.SOPOrderReturnID LEFT OUTER JOIN
                         dbo.SLCustomerAccount AS cust ON sop.CustomerID = cust.SLCustomerAccountID LEFT OUTER JOIN
                         dbo.StockItem AS si ON sopLine.ItemCode = si.Code LEFT OUTER JOIN
                         dbo.FUEL_ProductionPlanBatch AS bat ON ppi.ProductionPlanBatchID = bat.ProductionPlanBatchID LEFT OUTER JOIN
                         dbo.FUEL_Boxed AS boxed ON sopLine.SOPOrderReturnLineID = boxed.SopItemLineID AND pp.ProductionPlanID = boxed.ProductionPlanID
WHERE        (sopLine.AllocatedQuantity > 0)

这是我替换的代码,虽然我已经足够了,但事实并非如此。

 ISNULL(sopLine.AllocatedQuantity - boxed.QtyBoxed, 0) AS LineQty

enter image description here

请在我一生中从未使用过的计数表中忽略我的无知,问题是它导致某些产品未出现在结果集中,我们试图了解为什么这不再是老程序员的错误了和我们在一起。

0 个答案:

没有答案