2011-03-15 89 views
20

下面的过程给了我,当我调用它使用CALL语句错误:创建MySQL中的临时表存储过程


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获得了我的源代码。我只是无法弄清楚我做错了什么。

回答

18

我已经为你整理了一些并添加了示例代码。我始终保持我的参数名称与它们所代表的字段相同,但以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'); 
+0

看起来我的权利! – RolandoMySQLDBA 2011-03-15 08:20:55

+0

我创建临时表的目的是查询它之后。我刚刚尝试了您的代码,并且您创建的tmp表不能用于查询。相反,它给了我一个结果集。因此,该流程只准备员工考勤 – burntblark 2011-03-15 08:33:43

+0

您可以按照您的建议进行操作,但该表只能用于创建或称为sproc的连接。我不会推荐这种方法,但如果你可以详细说明你想要做什么,我可能会有其他一些想法。您可能还想查看http://pastie.org/1673574 – 2011-03-15 08:47:55

5

通过MySQL默认配置变量sql_notes设置为1

这意味着, DROP TEMPORARY TABLE IF EXISTS performance; 增量warning_count一个并在存储过程完成时收到警告。

您可以在my.cnf sql_notes变量设置为0或重写这样的存储过程:

CREATE DEFINER=`user`@`localhost` PROCEDURE `emp_performance`(id VARCHAR(10)) 
BEGIN 
SET @@session.sql_notes = 0; 
DROP TEMPORARY TABLE IF EXISTS performance; 
CREATE TEMPORARY TABLE performance AS 
    SELECT time_in, time_out, day FROM attendance WHERE employee_id = id; 
SET @@session.sql_notes = 1; 
END