2014-05-13 41 views
1

請檢查下面錯誤代碼:1137不能重新打開表:「amountforagents」

DELIMITER $$ 

    CREATE DEFINER=`ntc`@`%` PROCEDURE `new_procedure`(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())), 


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



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

(select @Position := @Position + 1 AS Rank from 
    collection_master ,(SELECT @Position := 0) r 
where AgentID=v_Agentid 
group by AgentID 
) as position; 
DROP TEMPORARY TABLE IF EXISTS amountforagents; 

create TEMPORARY table amountforagents (agentId int,DayAmount decimal,MonthAmount decimal,YearAmount decimal,Position int,totalamountreceived decimal); 
    set @agentId =v_Agentid; 
    update amountforagents SET totalamountreceived = (select ifnull(DayAmount,0)+ifnull(MonthAmount,0)+ifnull(YearAmount,0) from amountforagents where agentId=v_Agentid); 

INSERT Into amountforagents  
    (agentId,DayAmount,MonthAmount,YearAmount,Position,totalamountreceived) values(@agentId, 
@DayAmount,@MonthAmount,@YearAmount,@Position,@totalamountreceived); 


select agentId,DayAmount,MonthAmount,YearAmount,Position,totalamountreceived from amountforagents; 


END 

在這裏,我triing給我的順序通過SP爲totalamountreceived基於三列的總量在分配排名sp,這三列在temp表中,但它顯示錯誤錯誤代碼:1137無法重新打開表格:'amountforagents'

回答

4

問題可能與您的子選擇朝向底部,在UPDATE聲明中。

正如你可以閱讀here on the mysql docs

您不能引用到一個臨時表不止一次在同一 查詢。例如,下面不工作:

mysql> SELECT * FROM temp_table, temp_table AS t2; 
ERROR 1137: Can't reopen table: 'temp_table' 

如果您在 的指 臨時表中多次在不同 別名存儲功能,即使發生引用在不同的語句也會發生此錯誤功能。

您的UPDATE聲明似乎無法完成。您在創建表格後立即使用UPDATE(因此它將是空的),所以不會有任何問題。也許你的意思只是設置變量爲@totalamountreceived那裏呢?

嘗試刪除這一行:

update amountforagents 

然後修改SET語句來創建變量和值@totalamountreceived

SET @totalamountreceived = ifnull(@DayAmount, 0) 
    + ifnull(@MonthAmount, 0) 
    + ifnull(@YearAmount, 0); 

這應該給你你後的結果,除非我誤解了你想要達成的目標。

一起:

DELIMITER $$ 

    CREATE DEFINER=`ntc`@`%` PROCEDURE `new_procedure`(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())), 
    (select 
      @MonthAmount:=sum(AmountRecevied) as Totoalamountperday 
     from 
      collection_master 
     where 
      AgentID = v_Agentid 
       and date_time between DATE_FORMAT(NOW(), '%Y-%m-01') and LAST_DAY(now() - interval 0 month)), 
    (select 
      @YearAmount:=sum(AmountRecevied) as Totoalamountpermonth 
     from 
      collection_master 
     where 
      AgentID = v_Agentid 
       and year(Date_Time) = YEAR(CURRENT_DATE())), 
    (select 
      @Position:[email protected] + 1 AS Rank 
     from 
      collection_master, 
      (SELECT @Position:=0) r 
     where 
      AgentID = v_Agentid 
     group by AgentID) as position; 
DROP TEMPORARY TABLE IF EXISTS amountforagents; 

CREATE TEMPORARY TABLE amountforagents (agentId int,DayAmount decimal,MonthAmount decimal,YearAmount decimal,Position int,totalamountreceived decimal); 

SET @agentId = v_Agentid; 
SET @totalamountreceived = ifnull(@DayAmount, 0) 
    + ifnull(@MonthAmount, 0) 
    + ifnull(@YearAmount, 0); 

INSERT INTO amountforagents  
    (agentId,DayAmount,MonthAmount,YearAmount,Position,totalamountreceived) 
    VALUES(@agentId, 
@DayAmount,@MonthAmount,@YearAmount,@Position,@totalamountreceived); 

SELECT 
    agentId, 
    DayAmount, 
    MonthAmount, 
    YearAmount, 
    Position, 
    totalamountreceived 
FROM 
    amountforagents; 

END 
+0

不,我已經嘗試用SET @totalamountreceived我出空 – stpdevi

+0

您可以編輯您原來的職位,以顯示當你習慣了'set @ totalamountreceived'你嘗試過什麼碼?我在上面發佈的示例應該可以正常工作。 –

+0

與上面相同只是我刪除了更新amountforagents SET totalamountreceived =(選擇ifnull(DayAmount,0)+ ifnull(MonthAmount,0)+ ifnull(YearAmount,0)from amountforagents where agentId = v_Agentid);並添加了SET @totalamountreceived = ifnull(@DayAmount,0) + ifnull(@MonthAmount,0) + ifnull(@YearAmount,0); – stpdevi