错误代码:1137无法重新打开表:'amountforagents'

时间:2014-05-13 07:16:53

标签: mysql stored-procedures

请查看下面的SP

  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

在这里,我试图通过为totalamountreceived指定排序,根据sp列中的三列总数来分配排名,这三列是临时表。但它显示错误错误代码:1137无法重新打开表:' amountforagents'

1 个答案:

答案 0 :(得分:4)

问题可能出在您的UPDATE声明中的底部选择。

您可以阅读here on the mysql docs

  

您不能在同一个表中多次引用TEMPORARY表   查询。例如,以下内容不起作用:

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

如果您参考a,也会发生此错误   临时表在存储函数下多次不同   别名,即使引用发生在其中的不同语句中   功能。

您的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:=@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;
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