mysql - 在 MySQL 存储过程中创建临时表

标签 mysql sql stored-procedures

当我使用 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/

相关文章:

Mysql - 将3个具有不同行数的表连接到另一个表中

python - SQLAlchemy python DELETE记录,奇怪的模式

c# - 并发选择唯一记录

sql - 删除除最近 100 行之外的所有行

sql - 带有 WHERE 条件和 INNER JOIN 的 SELECT 命令

php - mysql php 中的 SQL 存储过程

php - 如何使用选择和输入根据发布数据过滤结果?

sql - 存储过程的命名约定是什么?

sql - 存储过程拒绝删除它创建的列

php - 如何向数据库中插入多个数组?