2017-05-05 32 views
-2

我在過程中創建臨時表,如下所示。如何在mysql中多次使用臨時表

create temporary table tmp_table 
(
id int, 
idate date, 
iname varchar(100) 
) 

insert into tmp_table 
select dept_no, dept_create_date,dept_name from dept_record_2016 
union all 
select dept_no, dept_create_date,dept_name from dept_record_2017; 


select * from tmp_table; 

insert into tmp_table 
select emp_no , date_of_join , emp_full_name from emp_info; 

我得到以下錯誤錯誤1137:無法重新打開表:'tmp_table'。

那麼如何在多次使用單個臨時表的過程中使用相同的臨時表?

有其他解決方案嗎?

+0

當你得到的錯誤。請編輯你的問題與錯誤消息。好問題! –

+0

https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem – e4c5

+0

@PhilippSander我已經在我的問題中提到,但錯誤1137:無法重新打開表:'tmp_table' – Smith

回答

0

編輯:

當我嘗試此過程重現你的問題,我不能複製。對我來說工作得很好。

delimiter $$ 
create procedure whatever() 
begin 
create temporary table tmp_table (a int, b int); 

insert into tmp_table 
select * from a 
union all 
select * from b; 

select * from tmp_table; 

insert into tmp_table 
select * from c; 
end $$ 
delimiter ; 

call whatever(); 

請詳細描述您的問題!並嘗試隔離何時出現錯誤消息。

原來的答覆:

我猜你的問題是這樣的:

臨時表可見只對當前會話,並且是當會話關閉 自動刪除。這意味着兩個不同的會話可以使用相同的臨時表名稱,而不會與 相互衝突,或者與同名的現有非臨時表 衝突。 (現有的表被隱藏,直到臨時表 被丟棄。)

Source (official manual)

+0

你能解釋一下爲什麼我不能一次又一次地用簡單的英文單詞訪問臨時表嗎? – Smith

+0

就像我在回答中所說的,我無法重現問題。我要求你提供更多的細節。你是否嘗試從不同的會話訪問表格?您的訪問嘗試之間還會發生什麼?無論你能提供什麼......否則我們無法幫助你。 – fancyPants

+0

我已經使用了一個具有「任何」過程名稱的示例,並且它只在一個會話中調用一次,而不是從另一個會話訪問。 – Smith