Question

Please check my SP below

  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

Here I am triing to give order by for totalamountreceived to assign rank based on total amount of three columns as in sp,those three columns are in temp table.but it's showing error Error Code: 1137 Can't reopen table: 'amountforagents'

Was it helpful?

Solution

The problem is likely with your sub-select towards the bottom, inside your UPDATE statement.

As you can read here on the mysql docs:

You cannot refer to a TEMPORARY table more than once in the same query. For example, the following does not work:

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

This error also occurs if you refer to a temporary table multiple times in a stored function under different aliases, even if the references occur in different statements within the function.

Your UPDATE statement appears to not do a whole lot anyway. You're using UPDATE on a table right after it's been created (so it will be empty) so there won't be any hows anyhow. Perhaps you meant to just set the variable for @totalamountreceived there instead?

Try removing this line:

update amountforagents 

Then modify your SET statement to create the variable and value for @totalamountreceived:

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

This should give you the result you're after, unless I've misunderstood what your'e trying to achieve.

All together:

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top