【问题标题】:SP working on localhost but not on live serverSP 在本地主机上工作,但不在实时服务器上
【发布时间】:2018-09-12 16:26:30
【问题描述】:

我在 SQL 服务器上创建了一个 SP:

CREATE PROC [dbo].[proc_GetSalesProductWise] @ItemCode int,
                                         @year int,  
                                         @CustomerCode numeric(18,0),  
                                         @Month int,
                                         @DataFrom nvarchar(15) AS
BEGIN

SELECT ISNULL((SELECT SUM(Quantity)
               FROM QryBoutiqueSalesGraphProductWise
               WHERE ItemCode = CASE WHEN ISNULL(@ItemCode,0)=0 THEN ItemCode
                                     ELSE @ItemCode
                                END
                 AND YEAR(InvDate) = @year
                 AND Month(InvDate) = @Month
                 AND DataFrom = @DataFrom
                 AND CustomerCode = @CustomerCode
               GROUP BY ItemCode),0) AS Quantity,
        ISNULL((SELECT SUM(GrossAmount)
                FROM QryBoutiqueSalesGraphProductWise
                WHERE ItemCode = CASE WHEN ISNULL(@ItemCode,0)=0 THEN ItemCode
                                      ELSE @ItemCode
                                 END
                  AND YEAR(InvDate) = @year
                  AND Month(InvDate) = @Month
                  AND DataFrom = @DataFrom
                  AND CustomerCode = @CustomerCode
               GROUP BY ItemCode),0) AS Amount

END

它在 Localhost 上运行良好,但在实时站点上出现以下错误。

System.Data.SqlClient.SqlException: Subquery returned more than 1 value. 
This is not permitted when the subquery follows =, !=, <, <= , >, >= or when 
the subquery is used as an expression.

谁能帮我解决这个问题或告诉我问题出在哪里?

【问题讨论】:

  • 您使用的是 MySQL 还是 MS SQL Server?
  • 错误的哪一部分你不明白?这很清楚(在我看来)。
  • 程序有问题吗?
  • jarlh 我正在使用 SQL Server
  • 如果所选数据的同一年、同一月和“客户代码”存在多个“项目代码”,您将收到错误消息。对于所选数据,此数据条件显然不存在于您的非产品环境中。您可以使用SELECT ItemCode, year(InvDate) AS year, Month(InvDate) AS month, CustomerCode FROM QryBoutiqueSalesGraphProductWise GROUP BY ItemCode, year(InvDate), Month(InvDate), CustomerCode HAVING COUNT(*) &gt; 1; 识别问题数据

标签: sql sql-server sql-server-2008 stored-procedures


【解决方案1】:

如果 itemcode 多于 onme is uniq,SQL 查询返回多于一行的可能性:

select sum(Quantity)
from QryBoutiqueSalesGraphProductWise where ItemCode = CASE WHEN 
ISNULL(@ItemCode,0)=0 THEN ItemCode ELSE @ItemCode END
and year(InvDate) = @year and Month(InvDate) = @Month and DataFrom = 
@DataFrom and CustomerCode = @CustomerCode
group by ItemCode

select sum(GrossAmount)
from QryBoutiqueSalesGraphProductWise where ItemCode = CASE WHEN 
ISNULL(@ItemCode,0)=0 THEN ItemCode ELSE @ItemCode END
and year(InvDate) = @year and Month(InvDate) = @Month and DataFrom = 
@DataFrom and CustomerCode = @CustomerCode

按项目代码分组

请尝试以下:

  Create Proc [dbo].[proc_GetSalesProductWise]
    @ItemCode int,
    @year int,  
    @CustomerCode numeric(18,0),  
    @Month int,
    @DataFrom nvarchar(15)

    AS
    Begin
    select ItemCode, sum(ISNULL(Quantity,0)) as Quantity, 
    sum(ISNULL(GrossAmount,0)) as Amount
    from QryBoutiqueSalesGraphProductWise where ItemCode = CASE WHEN 
    ISNULL(@ItemCode,0)=0 THEN ItemCode ELSE @ItemCode END
    and year(InvDate) = @year and Month(InvDate) = @Month and DataFrom = 
    @DataFrom and CustomerCode = @CustomerCode
    group by ItemCode
END

【讨论】:

  • 你刚刚打败了我。 :)
  • 我早先做了这个过程,但是当没有找到 Itemcode 的数据时,SP 返回空结果。这是我不想要的。我希望它在没有找到记录时返回)。
  • *我希望它在没有找到记录时返回 0。这就是我使用 ISNULL 的原因
【解决方案2】:

检查你传入的参数值。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-03-05
    • 1970-01-01
    • 1970-01-01
    • 2021-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多