当我使用 CALL 语句调用它时,以下过程给了我一个错误:
CREATE DEFINER=`user`@`localhost` PROCEDURE `emp_performance`(id VARCHAR(10))
BEGIN
DROP TEMPORARY TABLE IF EXISTS performance;
CREATE TEMPORARY TABLE performance AS
SELECT time_in, time_out, day FROM attendance WHERE employee_id = id;
END
错误提示“未知表'性能'”。
这是我第一次实际使用存储过程,我的资源来自 Google。我只是无法弄清楚我做错了什么。
最佳答案
我已经为您整理了一下,并添加了示例代码。我始终保持参数名称与它们所代表的字段相同,但前缀为 p_ 以防止出现问题。我对在 sproc 主体中声明的变量执行相同操作,但前缀为 v_。
你可以在这里找到我的另一个例子:
Generating Depth based tree from Hierarchical Data in MySQL (no CTEs)
drop procedure if exists emp_performance;
delimiter #
create procedure emp_performance
(
in p_employee_id varchar(10)
)
begin
declare v_counter int unsigned default 0;
create temporary table tmp engine=memory select time_in, time_out
from attendance where employee_id = p_employee_id;
-- do stuff with tmp...
select count(*) into v_counter from tmp;
-- output and cleanup
select * from tmp order by time_in;
drop temporary table if exists tmp;
end#
delimiter ;
call emp_performance('E123456789');
关于mysql - 在 MySQL 存储过程中创建临时表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5308969/