SQL Server视图生成不同的结果集

时间:2011-11-30 11:08:03

标签: sql-server view

我有一个SQL Server视图:

SELECT SOPOrderReturn.DocumentNo,
       StockItem.Code,
       SLCustomerAccount.CustomerAccountNumber,
       SOPOrderReturn.CustomerID, 
       SOPDocDelAddress.PostalName,
       SOPDocDelAddress.AddressLine1,
       SOPDocDelAddress.AddressLine2,
       SOPDocDelAddress.AddressLine3, 
       SOPDocDelAddress.AddressLine4,
       SOPDocDelAddress.PostCode,
       SOPOrderReturnLine.LineQuantity,
       SOPOrderReturn.PromisedDeliveryDate, 
       StockItem.SpareNumber2,
       StockItem.SpareNumber3,
       StockItem.Name,
       StockItem.SpareText2,
       StockItem.SpareText1, 
       StockItem.SpareNumber1
FROM   SOPOrderReturn
       INNER JOIN SOPOrderReturnLine
          ON SOPOrderReturn.SOPOrderReturnID = SOPOrderReturnLine.SOPOrderReturnID
       INNER JOIN SOPDocDelAddress
          ON SOPOrderReturn.SOPOrderReturnID = SOPDocDelAddress.SOPOrderReturnID
       INNER JOIN SLCustomerAccount
          ON SOPOrderReturn.CustomerID = SLCustomerAccount.SLCustomerAccountID
       INNER JOIN StockItem
          ON SOPOrderReturnLine.ItemCode = StockItem.Code
WHERE  (NOT (StockItem.Code LIKE '%DELIVERY%')) AND
       (NOT (StockItem.Name LIKE '%DELIVERY%')) AND 
       (SOPOrderReturn.PromisedDeliveryDate > CURRENT_TIMESTAMP - 3)

使用此查询从.net调用'原样'时:

SELECT [DocumentNo],[Code],
       [CustomerAccountNumber],
       [CustomerID],[PostalName],
       [AddressLine1],[AddressLine2],
       [AddressLine3],[AddressLine4],
       [PostCode],[LineQuantity],
       [PromisedDeliveryDate],
       [SpareNumber2],
       [SpareNumber3],
       [Name],
       [SpareText2],
       [SpareText1],
       [SpareNumber1]
FROM [viwSalesOrdersRecent];

......一切都很笨拙。它返回我希望看到的所有861行,这与SQL Server在View中应该说的相匹配。

但是,当我调用View(如上所述)并在其上添加以下Where子句时:

WHERE [DocumentNo] = @prmSalesOrderNumber AND [Code] = @prmStockCode;

查询返回整个数据集,忽略View中现有的Where子句,我不明白这种行为。我已经没有使用SQL Server了,已经3年没碰过它了,而且我现在已经回到了它的深处并完全脱离了我的深度。

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:1)

最终我从来没有解决过这个问题,所以我最终把它放到了微软,让他们看看。

他们暂时确认这是由SQL Server中的错误引起的,他们希望通过将来的更新来解决这个问题。

答案 1 :(得分:0)

我建议您在视图中添加isnull以及在视图中查询。空值会在很大程度上影响结果集。 检查以下代码

        SELECT SOPOrderReturn.DocumentNo,
               StockItem.Code,
               SLCustomerAccount.CustomerAccountNumber,
               SOPOrderReturn.CustomerID, 
               SOPDocDelAddress.PostalName,
               SOPDocDelAddress.AddressLine1,
               SOPDocDelAddress.AddressLine2,
               SOPDocDelAddress.AddressLine3, 
               SOPDocDelAddress.AddressLine4,
               SOPDocDelAddress.PostCode,
               SOPOrderReturnLine.LineQuantity,
               SOPOrderReturn.PromisedDeliveryDate, 
               StockItem.SpareNumber2,
               StockItem.SpareNumber3,
               StockItem.Name,
               StockItem.SpareText2,
               StockItem.SpareText1, 
               StockItem.SpareNumber1
        FROM   SOPOrderReturn
               INNER JOIN SOPOrderReturnLine
                  ON SOPOrderReturn.SOPOrderReturnID = SOPOrderReturnLine.SOPOrderReturnID
               INNER JOIN SOPDocDelAddress
                  ON SOPOrderReturn.SOPOrderReturnID = SOPDocDelAddress.SOPOrderReturnID
               INNER JOIN SLCustomerAccount
                  ON SOPOrderReturn.CustomerID = SLCustomerAccount.SLCustomerAccountID
               INNER JOIN StockItem
                  ON SOPOrderReturnLine.ItemCode = StockItem.Code
        WHERE  (NOT (isnull(StockItem.Code,'') LIKE '%DELIVERY%')) AND
               (NOT (isnull(StockItem.Name,'') LIKE '%DELIVERY%')) AND 
               (SOPOrderReturn.PromisedDeliveryDate > CURRENT_TIMESTAMP - 3)



        SELECT [DocumentNo],[Code],
               [CustomerAccountNumber],
               [CustomerID],[PostalName],
               [AddressLine1],[AddressLine2],
               [AddressLine3],[AddressLine4],
               [PostCode],[LineQuantity],
               [PromisedDeliveryDate],
               [SpareNumber2],
               [SpareNumber3],
               [Name],
               [SpareText2],
               [SpareText1],
               [SpareNumber1]
        FROM [viwSalesOrdersRecent]
        WHERE isnull([DocumentNo],'') = isnull(@prmSalesOrderNumber,'')
         AND isnull([Code],'') = isnull(@prmStockCode,'')