无法将参数传递给存储过程

时间:2014-05-12 05:43:06

标签: mysql

我要求计算日,月和年金额之和。我写了如下的存储过程。

DELIMITER $$

CREATE DEFINER=`ntc`@`%` PROCEDURE `AgentRank`(IN Agentid int)

BEGIN
    select (select  @DayAmount := sum(AmountRecevied) as Totoalamountperday from 
        collection_master  
        where  AgentID=Agentid and  day(Date_Time)= day(CURRENT_DATE()) 
        group by AgentID
    ) as Dayamount,

    (select  @MonthAmount :=sum(AmountRecevied) as Totoalamountperday  from 
        collection_master
        where  AgentID=Agentid and date_time between DATE_FORMAT(NOW() ,'%Y-%m-01') and LAST_DAY(now() - interval 0 month )
        group by AgentID
    ) as monthamount,

    (select   @YearAmount := sum(AmountRecevied) as Totoalamountpermonth  from 
        collection_master  
        where AgentID=Agentid  and year(Date_Time) =YEAR(CURRENT_DATE())
        group by AgentID
    ) as yearamount,

    (select @Position := @Position + 1 AS Rank  from 
        collection_master ,(SELECT @Position := 0) r
        where AgentID=Agentid
        group by AgentID
    ) as position;

END

当我执行存储过程时,我的输出低于输出enter image description here

当我在商店程序的个别查询中执行时,我得到正确的输出,如下面的enter image description here

请检查一次我错了吗?

1 个答案:

答案 0 :(得分:1)

这是存储过程的前几行:

CREATE DEFINER=`ntc`@`%` PROCEDURE `AgentRank`(IN Agentid int)
BEGIN
    select (select  @DayAmount := sum(AmountRecevied) as Totoalamountperday
            from collection_master  
            where  AgentID=Agentid and  day(Date_Time)= day(CURRENT_DATE()) 
-------------------^
            group by AgentID
           ) as Dayamount,

指向表达式是重言式。也就是说,它将列AgentId与自身进行比较。问题是你的参数与列名相同,坏,坏,坏,坏主意。这是一个修复:

CREATE DEFINER=`ntc`@`%` PROCEDURE `AgentRank`(IN v_Agentid int)
BEGIN
    select (select  @DayAmount := sum(AmountRecevied) as Totoalamountperday
            from collection_master  
            where AgentID = v_Agentid and day(Date_Time)= day(CURRENT_DATE()) 
           ) as Dayamount,

另请注意,您不需要聚合。子查询必须返回零行或一行(否则会出错)。您只选择了一个AgentId,因此请移除group by以防止误解。