簡體   English   中英

無法將參數傳遞給存儲過程

[英]Unable to pass parameter to stored procedure

我要求計算日,月和年金額的總和。 我寫了如下存儲過程。

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

當我執行存儲過程時,我的輸出低於輸出 在此處輸入圖片說明

當我在存儲過程的單個查詢中執行時,我得到如下正確的輸出 在此處輸入圖片說明

請檢查一次,哪里出了錯?

這是存儲過程的前幾行:

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以防止誤解。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM