添加字段后查询停止工作

时间:2016-10-25 17:19:58

标签: sql asp.net-mvc stored-procedures

我有这个存储过程(我没有这样做):

USE [RA_DB]
GO
/****** Object:  StoredProcedure [dbo].[GetLogWasteReport]    *******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[GetLogWasteReport]
    -- Add the parameters for the stored procedure here
    @IdLogWaste int,
    @initDate DateTime,
    @endDate DateTime
AS
BEGIN
SET FMTONLY OFF
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;
  SELECT lwd.Manifest
 ,SUM(CASE WHEN lwd.Manifest IS NULL THEN 0 ELSE  lwd.Quantity END) AS TOTAL
  ,COUNT(lwd.Id) AS Count
  INTO #TEMPTotal
 FROM [dbo].[LogWaste] lw (NOLOCK)
    INNER JOIN [dbo].[LogWasteDetail] lwd (NOLOCK)
        ON lw.Id = lwd.IdLogWaste
--WHERE
--          lw.Id = @IdLogWaste
WHERE 
lwd.OutputDate IS NOT NULL
AND
lwd.IdWaste = @IdLogWaste
AND
lwd.OutputDate BETWEEN @initDate AND @endDate
 GROUP BY lwd.Manifest



 SELECT
    w.Name AS Waste
    ,a.Description AS Area
    ,lwd.Quantity
    , CONVERT(VARCHAR(12),lwd.InitDate,101) AS InitDate
    , CONVERT(VARCHAR(12),lwd.OutputDate,101) AS OutputDate
    ,lwd.Balance
    ,lwd.Manifest
    ,temp.TOTAL
    ,temp.Count
    FROM [dbo].[LogWaste] lw (NOLOCK)
    INNER JOIN [dbo].[LogWasteDetail] lwd (NOLOCK)
        ON lw.Id = lwd.IdLogWaste
    INNER JOIN [dbo].[Waste] w (NOLOCK)
        ON lwd.IdWaste = w.Id
    INNER JOIN [dbo].[Area] a (NOLOCK)
        ON a.Id = lwd.IdArea
    INNER JOIN #TEMPTotal temp 
        ON lwd.Manifest = temp.Manifest OR (lwd.Manifest IS NULL AND temp.Manifest IS NULL)
     --WHERE
        --  lw.Id = @IdLogWaste
    WHERE 
        lwd.OutputDate IS NOT NULL
        AND
        lwd.IdWaste = @IdLogWaste
        AND
        lwd.OutputDate BETWEEN @initDate AND @endDate
    ORDER BY lwd.Manifest,lwd.InitDate 


   DROP TABLE #TEMPTotal



END

我想要做的是添加我可以从[LogWaste]获得的字段“BranchOfficeId”(有BranchOfficeIdFk),我需要的是什么 在这家商店选择这个? 我尝试了像

这样的东西
USE [SA_DB]
GO
/****** Object:  StoredProcedure [dbo].[GetLogWasteReport]  ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[GetLogWasteReport]
    -- Add the parameters for the stored procedure here
    @IdLogWaste int,
    @initDate DateTime,
    @endDate DateTime,
    @branchOfficeId Int
AS
BEGIN
SET FMTONLY OFF
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;
  SELECT lwd.Manifest
 ,SUM(CASE WHEN lwd.Manifest IS NULL THEN 0 ELSE  lwd.Quantity END) AS TOTAL
  ,COUNT(lwd.Id) AS Count
  INTO #TEMPTotal
 FROM [dbo].[LogWaste] lw (NOLOCK)
    INNER JOIN [dbo].[LogWasteDetail] lwd (NOLOCK)
        ON lw.Id = lwd.IdLogWaste
--WHERE
--          lw.Id = @IdLogWaste
WHERE 
lwd.OutputDate IS NOT NULL
AND
lwd.IdWaste = @IdLogWaste
AND
lwd.OutputDate BETWEEN @initDate AND @endDate
 GROUP BY lwd.Manifest



 SELECT
    w.Name AS Waste
    ,a.Description AS Area
    ,lwd.Quantity
    , CONVERT(VARCHAR(12),lwd.InitDate,101) AS InitDate
    , CONVERT(VARCHAR(12),lwd.OutputDate,101) AS OutputDate
    ,lwd.Balance
    ,lwd.Manifest
    ,lw.BranchOfficeId
    ,temp.TOTAL
    ,temp.Count
    FROM [dbo].[LogWaste] lw (NOLOCK)
    INNER JOIN [dbo].[LogWasteDetail] lwd (NOLOCK)
        ON lw.Id = lwd.IdLogWaste
    INNER JOIN [dbo].[Waste] w (NOLOCK)
        ON lwd.IdWaste = w.Id
    INNER JOIN [dbo].[Area] a (NOLOCK)
        ON a.Id = lwd.IdArea
    INNER JOIN #TEMPTotal temp 
        ON lwd.Manifest = temp.Manifest OR (lwd.Manifest IS NULL AND temp.Manifest IS NULL)
     --WHERE
        --  lw.Id = @IdLogWaste
    WHERE 
        lwd.OutputDate IS NOT NULL
        AND
        lw.BranchOfficeId = @branchOfficeId
        AND
        lwd.IdWaste = @IdLogWaste
        AND
        lwd.OutputDate BETWEEN @initDate AND @endDate
    ORDER BY lwd.Manifest,lwd.InitDate 


   DROP TABLE #TEMPTotal



END

但它不起作用,我的查询总是返回值0,任何人都可以帮助我吗?

查询(前):

result = context.GetLogWasteReport(id, initDate, endDate).ToList();

查询(后):

result = context.GetLogWasteReport(id, initDate, endDate, branchOfficeId).ToList();

注意:在我添加字段之前,branchOfficeId查询工作并获取值

2 个答案:

答案 0 :(得分:0)

我敢打赌你的@branchOfficeId没有正确设置并且会以0结束。我会确保它设置为你认为它被设置为的位置。

如果你打算允许零值表示没有选择,那么只需更新你的where子句。

(@branchOfficeId=0 OR lw.BranchOfficeId = @branchOfficeId)

如果允许空...

(@branchOfficeId IS NULL OR lw.BranchOfficeId = @branchOfficeId)

答案 1 :(得分:0)

由于没有匹配项,因此没有结果。这可能是因为值为NULL

如果这是问题所在,您可以使用以下方法修复:

   (lw.BranchOfficeId = @branchOfficeId or @branchOfficeId is NULL)