Mysql 错误代码:1137无法重新打开表:“amountforagents”
请在下面查看我的SPMysql 错误代码:1137无法重新打开表:“amountforagents”,mysql,stored-procedures,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_Tim
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中三列的总数分配排名,这三列在temp表中。但它显示错误代码:1137无法重新打开表:“amountforagents”问题可能是UPDATE语句中的子选择位于底部 正如你所看到的: 同一时间内不能多次引用临时表 查询例如,以下操作不起作用:
mysql> SELECT * FROM temp_table, temp_table AS t2;
ERROR 1137: Can't reopen table: 'temp_table'
如果您引用了
在不同的存储函数中多次使用临时表
别名,即使引用出现在
功能
不管怎样,您的UPDATE语句似乎并没有完成很多工作。您在创建表之后立即对其使用更新,因此表将为空,因此不会有任何方式。也许您只是想在此处设置@TotalAmonReceived的变量
尝试删除此行:
update amountforagents
然后修改SET语句,为@TotalAmonReceived创建变量和值:
这应该会给你你想要的结果,除非我误解了你想要达到的目标
总而言之:
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
不,我已经用SET@totalamunreceived I尝试过了,显示为空。您可以编辑您的原始帖子,显示您在使用SET@totalamunreceived时尝试过的代码吗?我上面发布的示例应该可以正常工作。与上面相同,我删除了更新amountforagents SET totalamountreceived=select ifnulldaymount、0+ifnullMonthAmount、0+ifnullYearAmount、0 from amountforagents,其中agentId=v_agentId;并添加集合@TotalAmonReceived=ifnull@DayAmount, 0 + ifnull@MonthAmount, 0 + ifnull@YearAmount, 0;@stpdevi我添加了您应该运行的整个代码块,作为我答案的一部分。我想你可能编辑错了,因为当我在本地运行它时,示例是有效的。嗨,alex,我在这里得到了总数,但需要显示基于代理总数的排名如何做到这一点?
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