stp*_*evi 2 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
Run Code Online (Sandbox Code Playgroud)
在这里,我试图为 totalamountreceived 下订单,以根据 sp 中三列的总数分配排名,这三列在临时表中。但它显示错误错误代码:1137 无法重新打开表:'amountforagents'
问题可能出在您的子选择底部,在您的UPDATE语句中。
正如您在 mysql 文档中所读到的:
在同一个查询中不能多次引用 TEMPORARY 表。例如,以下不起作用:
Run Code Online (Sandbox Code Playgroud)mysql> SELECT * FROM temp_table, temp_table AS t2; ERROR 1137: Can't reopen table: 'temp_table'如果您在不同别名下的存储函数中多次引用临时表,即使引用出现在函数内的不同语句中,也会发生此错误。
UPDATE无论如何,您的声明似乎并没有做很多事情。您在表创建后立即在表上使用 UPDATE(因此它将为空),因此无论如何都不会有任何方法。也许你的意思是只为@totalamountreceived那里设置变量?
尝试删除此行:
update amountforagents
Run Code Online (Sandbox Code Playgroud)
然后修改您的SET语句以创建变量和值 @totalamountreceived:
SET @totalamountreceived = ifnull(@DayAmount, 0)
+ ifnull(@MonthAmount, 0)
+ ifnull(@YearAmount, 0);
Run Code Online (Sandbox Code Playgroud)
这应该给你你想要的结果,除非我误解了你想要达到的目标。
全部一起:
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
Run Code Online (Sandbox Code Playgroud)