SQL View未运行结果,但正在运行的查询使视图确实返回了结果

时间:2018-11-29 17:41:19

标签: sql sql-server

我有一个视图,其中总共有大约10个左连接。

如果我直接执行视图,则会得到结果,但结果并不完整。如果我直接执行构成视图的查询,则会得到完整的结果。

我运行了sp_refreshView,问题仍然存在。

我从来没有遇到过这样的问题,而且我不确定要怎么解决。有人可以采取任何潜在的解决方案或故障排除步骤来尝试解决此问题吗?

我可以确认两个查询都针对同一个数据库运行。该数据库位于我的SQL Server本地实例上,它是我可以运行的唯一数据库。

我使用以下查询调用视图,但未获得结果:

SELECT *  
FROM [DHTraxxDev].[Inventory].[vwInventoryList]  
WHERE ProjectID = 46

这适用于某些项目编号,不适用于其他项目编号。

如果我在创建视图的ps.ProjectID = 46的末尾添加and子句,则可以获得相关信息。

用于创建视图的代码是

CREATE VIEW [Inventory].[vwInventoryList]
AS
    SELECT 
        ps.ID AS ProjectSpecID, 
        psrd.id AS 'ProjectSpecReceiveDetailId', 
        ps.ModelNumber, 
        p.Identifier, p.ProjectName, p.SiteName, 
        ps.ProjectID, ps.SpecNumber, ps.ProjectSpecImportFileID, 
        ps.GenericDescription, ps.DetailedDescription, 
        ps.LeadTime, ps.Quarter, ps.QuarterYear, 
        ps.ESACategory, ps.Scope AS ESAScope, 
        ps.GLCode AS ESAGLCode, ps.ShipToDestinationId, 
        ps.StatusID AS ProjectSpecStatus, 
        ISNULL(ps.AtticQuantity, 0) AS AtticQuantity, 
        ISNULL(ps.Quantity, 0) AS Quantity, 
        ISNULL(ps.AtticQuantity, 0) + ISNULL(ps.Quantity, 0) AS TotalQuantity, 
        ISNULL(ps.UnitCost, 0) AS UnitCost, 
        v.Name AS Vendor, luc.Description AS UnitType, 
        ps.EstimatedDeliveryDate, ps.EstimatedShippingDate, 
        ps.ProcurementNotes, 
        lpsl.Description AS 'ShipTo',
        psc.Description AS 'ComponentDescription', 
        po.ID AS PoId, po.StatusID AS POStatus, 
        po.Identifier AS PurchaseOrderIdentifier, 
        psc.ID AS ProjectSpecComponentID, 
        psc.SpecNumber AS ComponentSpecNumber, 
        psc.Multiplier,
        (ISNULL(ps.AtticQuantity, 0) + ISNULL(ps.Quantity, 0)) + COALESCE(psrToVendor.ToVendor, 0) - COALESCE(psrFromVendor.FromVendor, 0) AS 'QuantityAtVendor',
        COALESCE(psrToWarehouse.ToWarehouse, 0) - COALESCE(psrFromWarehouse.FromWarehouse, 0) AS QuantityAtWarehouse,
        COALESCE(psrToSite.ToSite, 0) - COALESCE(psrFromSite.FromSite, 0) AS QuantityAtSite,
        COALESCE(psrComponentToWarehouse.ToWarehouse, 0) - COALESCE(psrComponentFromWarehouse.FromWarehouse, 0) AS ComponentQuantityAtWarehouse,
        COALESCE(psrComponentToSite.ToSite, 0) - COALESCE(psrComponentFromSite.FromSite, 0) AS ComponentQuantityAtSite,
        (ISNULL(ps.AtticQuantity, 0) + ISNULL(ps.Quantity, 0)) * ISNULL(ps.UnitCost, 0) AS TotalCost, 
        (COALESCE(psrToWarehouse.ToWarehouse, 0) - COALESCE(psrFromWarehouse.FromWarehouse, 0)) * ISNULL(ps.UnitCost, 0) AS WarehouseCost, 
        (COALESCE(psrToSite.ToSite, 0) - COALESCE(psrFromSite.FromSite, 0)) * ISNULL(ps.UnitCost, 0) AS SiteCost,
        COALESCE(pendingInventory.Pending, 0) AS Pending,
        COALESCE(allocatedInventory.Allocated, 0) AS Allocated
    FROM            
        Project.ProjectSpec AS ps 
    INNER JOIN
        Project.Project AS p ON p.ID = ps.ProjectID 
    LEFT OUTER JOIN
        Project.lkupProjectSpecLocation AS lpsl ON lpsl.ID = ps.ShipToDestinationId 
    LEFT OUTER JOIN
        DH.Vendor AS v ON ps.VendorID = v.ID 
    LEFT OUTER JOIN
        PurchaseOrder.lkupUnitCost AS luc ON luc.ID = ps.UnitCostID 
    LEFT OUTER JOIN
        Project.ProjectSpecComponent AS psc ON psc.ProjectSpecID = ps.ID 
    LEFT OUTER JOIN
        PurchaseOrder.PurchaseOrder AS po ON po.ID = ps.PurchaseOrderID 
    LEFT OUTER JOIN
        Project.ProjectSpecReceivedDetails psrd on psrd.ProjectSpecID = ps.ID 
    LEFT JOIN 
        (SELECT  
             ProjectSpecID, SUM(ri.amount) AS 'Pending'
         FROM
             Inventory.Request r 
         INNER JOIN
             inventory.requestinventory ri ON r.id = ri.RequestID
         WHERE 
             RequestTypeID = 1 AND r.Deleted = 0 AND ri.Deleted = 0 
             AND RequestStatusID = 3
         GROUP BY
             ProjectSpecID) pendingInventory ON pendingInventory.ProjectSpecID = ps.ID
     LEFT JOIN 
         (SELECT
              ProjectSpecID, SUM(ri.amount) AS 'Allocated'
          FROM 
              Inventory.Request r 
          INNER JOIN
              inventory.requestinventory ri ON r.id = ri.RequestID
          WHERE
              RequestTypeID = 2 AND r.Deleted = 0 
              AND ri.Deleted = 0 AND RequestStatusID = 4
          GROUP BY
              ProjectSpecID) allocatedInventory ON allocatedInventory.ProjectSpecID = ps.ID
    LEFT JOIN
        (SELECT
             psrd.ProjectSpecID, psrd.ProjectSpecComponentID,
             ToSite = SUM(psrd.QuantityReceived)
         FROM 
             Project.ProjectSpecReceivedDetails psrd
         WHERE
             psrd.Deleted = 0
             AND psrd.ToProjectSpecLocationID = 3
             AND psrd.ProjectSpecComponentID IS NOT NULL
         GROUP BY 
             psrd.ProjectSpecID, psrd.ProjectSpecComponentID) psrComponentToSite ON psrComponentToSite.ProjectSpecID = ps.ID
    LEFT JOIN
        (SELECT
             psrd.ProjectSpecID, psrd.ProjectSpecComponentID
        ,FromSite = SUM(psrd.QuantityReceived)
    FROM Project.ProjectSpecReceivedDetails psrd
    WHERE
        psrd.Deleted = 0
        AND psrd.FromProjectSpecLocationID = 3
        and psrd.ProjectSpecComponentID is not null
    GROUP BY psrd.ProjectSpecID, psrd.ProjectSpecComponentID
) psrComponentFromSite ON psrComponentFromSite.ProjectSpecID = ps.ID
LEFT JOIN
(
    SELECT
        psrd.ProjectSpecID, psrd.ProjectSpecComponentID
        ,ToWarehouse = SUM(psrd.QuantityReceived)
    FROM Project.ProjectSpecReceivedDetails psrd
    WHERE
        psrd.Deleted = 0
        AND psrd.ToProjectSpecLocationID = 2
        and psrd.ProjectSpecComponentID is not null
    GROUP BY psrd.ProjectSpecID, psrd.ProjectSpecComponentID
) psrComponentToWarehouse ON psrComponentToWarehouse.ProjectSpecID = ps.ID
LEFT JOIN
(
    SELECT
        psrd.ProjectSpecID, psrd.ProjectSpecComponentID
        ,FromWarehouse = SUM(psrd.QuantityReceived)
    FROM Project.ProjectSpecReceivedDetails psrd
    WHERE
        psrd.Deleted = 0
        AND psrd.FromProjectSpecLocationID = 2
        and psrd.ProjectSpecComponentID is not null
    GROUP BY psrd.ProjectSpecID, psrd.ProjectSpecComponentID
) psrComponentFromWarehouse ON psrComponentFromWarehouse.ProjectSpecID = ps.ID
LEFT JOIN
(
    SELECT
        psrd.ProjectSpecID
        ,ToSite = SUM(psrd.QuantityReceived)
    FROM Project.ProjectSpecReceivedDetails psrd
    WHERE
        psrd.Deleted = 0
        AND psrd.ToProjectSpecLocationID = 3
        and psrd.ProjectSpecComponentID is null
    GROUP BY psrd.ProjectSpecID
) psrToSite ON psrToSite.ProjectSpecID = ps.ID
LEFT JOIN
(
    SELECT
        psrd.ProjectSpecID
        ,FromSite = SUM(psrd.QuantityReceived)
    FROM Project.ProjectSpecReceivedDetails psrd
    WHERE
        psrd.Deleted = 0
        AND psrd.FromProjectSpecLocationID = 3
        and psrd.ProjectSpecComponentID is null
    GROUP BY psrd.ProjectSpecID
) psrFromSite ON psrFromSite.ProjectSpecID = ps.ID
LEFT JOIN
(
    SELECT
        psrd.ProjectSpecID
        ,ToWarehouse = SUM(psrd.QuantityReceived)
    FROM Project.ProjectSpecReceivedDetails psrd
    WHERE
        psrd.Deleted = 0
        AND psrd.ToProjectSpecLocationID = 2
        and psrd.ProjectSpecComponentID is null
    GROUP BY psrd.ProjectSpecID
) psrToWarehouse ON psrToWarehouse.ProjectSpecID = ps.ID
LEFT JOIN
(
    SELECT
        psrd.ProjectSpecID
        ,FromWarehouse = SUM(psrd.QuantityReceived)
    FROM Project.ProjectSpecReceivedDetails psrd
    WHERE
        psrd.Deleted = 0
        AND psrd.FromProjectSpecLocationID = 2
        and psrd.ProjectSpecComponentID is null
    GROUP BY psrd.ProjectSpecID
) psrFromWarehouse ON psrFromWarehouse.ProjectSpecID = ps.ID
LEFT JOIN
(
    SELECT
        psrd.ProjectSpecID
        ,ToVendor = SUM(psrd.QuantityReceived)
    FROM Project.ProjectSpecReceivedDetails psrd
    WHERE
        psrd.Deleted = 0
        AND psrd.ToProjectSpecLocationID = 1
        and psrd.ProjectSpecComponentID is null
    GROUP BY psrd.ProjectSpecID
) psrToVendor ON psrToVendor.ProjectSpecID = ps.ID
LEFT JOIN
(
    SELECT
        psrd.ProjectSpecID
        ,FromVendor = SUM(psrd.QuantityReceived)
    FROM Project.ProjectSpecReceivedDetails psrd
    WHERE
        psrd.Deleted = 0
        AND psrd.FromProjectSpecLocationID = 1
        and psrd.ProjectSpecComponentID is null
    GROUP BY psrd.ProjectSpecID
) psrFromVendor ON psrFromVendor.ProjectSpecID = ps.ID
WHERE        (ps.Deleted = 0) AND (psc.ID IS NULL) OR
                         (ps.Deleted = 0) AND (psc.Deleted = 0)
GO

EXEC sys.sp_addextendedproperty @name=N'MS_DiagramPane1', @value=N'[0E232FF0-B466-11cf-A24F-00AA00A3EFFF, 1.00]
Begin DesignProperties = 
   Begin PaneConfigurations = 
      Begin PaneConfiguration = 0
         NumPanes = 4
         Configuration = "(H (1[40] 4[20] 2[20] 3) )"
      End
      Begin PaneConfiguration = 1
         NumPanes = 3
         Configuration = "(H (1 [50] 4 [25] 3))"
      End
      Begin PaneConfiguration = 2
         NumPanes = 3
         Configuration = "(H (1 [50] 2 [25] 3))"
      End
      Begin PaneConfiguration = 3
         NumPanes = 3
         Configuration = "(H (4 [30] 2 [40] 3))"
      End
      Begin PaneConfiguration = 4
         NumPanes = 2
         Configuration = "(H (1 [56] 3))"
      End
      Begin PaneConfiguration = 5
         NumPanes = 2
         Configuration = "(H (2 [66] 3))"
      End
      Begin PaneConfiguration = 6
         NumPanes = 2
         Configuration = "(H (4 [50] 3))"
      End
      Begin PaneConfiguration = 7
         NumPanes = 1
         Configuration = "(V (3))"
      End
      Begin PaneConfiguration = 8
         NumPanes = 3
         Configuration = "(H (1[56] 4[18] 2) )"
      End
      Begin PaneConfiguration = 9
         NumPanes = 2
         Configuration = "(H (1 [75] 4))"
      End
      Begin PaneConfiguration = 10
         NumPanes = 2
         Configuration = "(H (1[66] 2) )"
      End
      Begin PaneConfiguration = 11
         NumPanes = 2
         Configuration = "(H (4 [60] 2))"
      End
      Begin PaneConfiguration = 12
         NumPanes = 1
         Configuration = "(H (1) )"
      End
      Begin PaneConfiguration = 13
         NumPanes = 1
         Configuration = "(V (4))"
      End
      Begin PaneConfiguration = 14
         NumPanes = 1
         Configuration = "(V (2))"
      End
      ActivePaneConfig = 0
   End
   Begin DiagramPane = 
      Begin Origin = 
         Top = 0
         Left = 0
      End
      Begin Tables = 
         Begin Table = "ps"
            Begin Extent = 
               Top = 6
               Left = 38
               Bottom = 136
               Right = 295
            End
            DisplayFlags = 280
            TopColumn = 0
         End
         Begin Table = "p"
            Begin Extent = 
               Top = 6
               Left = 333
               Bottom = 136
               Right = 572
            End
            DisplayFlags = 280
            TopColumn = 0
         End
         Begin Table = "lpsl"
            Begin Extent = 
               Top = 6
               Left = 610
               Bottom = 102
               Right = 780
            End
            DisplayFlags = 280
            TopColumn = 0
         End
         Begin Table = "v"
            Begin Extent = 
               Top = 6
               Left = 818
               Bottom = 136
               Right = 1014
            End
            DisplayFlags = 280
            TopColumn = 0
         End
         Begin Table = "luc"
            Begin Extent = 
               Top = 102
               Left = 610
               Bottom = 215
               Right = 780
            End
            DisplayFlags = 280
            TopColumn = 0
         End
         Begin Table = "psc"
            Begin Extent = 
               Top = 138
               Left = 38
               Bottom = 268
               Right = 295
            End
            DisplayFlags = 280
            TopColumn = 0
         End
         Begin Table = "po"
            Begin Extent = 
               Top = 138
               Left = 333
               Bottom = 268
               Right = 549
            End
            DisplayFlags = 280
            TopColumn = 0
       ' , @level0type=N'SCHEMA',@level0name=N'Inventory', @level1type=N'VIEW',@level1name=N'vwInventoryList'
GO

EXEC sys.sp_addextendedproperty @name=N'MS_DiagramPane2', @value=N'  End
         Begin Table = "rdi"
            Begin Extent = 
               Top = 138
               Left = 818
               Bottom = 268
               Right = 1044
            End
            DisplayFlags = 280
            TopColumn = 0
         End
      End
   End
   Begin SQLPane = 
   End
   Begin DataPane = 
      Begin ParameterDefaults = ""
      End
   End
   Begin CriteriaPane = 
      Begin ColumnWidths = 11
         Column = 1440
         Alias = 900
         Table = 1170
         Output = 720
         Append = 1400
         NewValue = 1170
         SortType = 1350
         SortOrder = 1410
         GroupBy = 1350
         Filter = 1350
         Or = 1350
         Or = 1350
         Or = 1350
      End
   End
End
' , @level0type=N'SCHEMA',@level0name=N'Inventory', @level1type=N'VIEW',@level1name=N'vwInventoryList'
GO

EXEC sys.sp_addextendedproperty @name=N'MS_DiagramPaneCount', @value=2 , @level0type=N'SCHEMA',@level0name=N'Inventory', @level1type=N'VIEW',@level1name=N'vwInventoryList'
GO

1 个答案:

答案 0 :(得分:0)

我会尝试重写WHERE子句:

WHERE (ps.Deleted = 0) AND COALESCE(psc.Deleted, 0) = 0