使用临时表时的MySQL错误 - “无法重新打开表:'temporary_name'”

0 投票
2 回答
4106 浏览
提问于 2025-04-16 22:57

我正在用Python调用一个存储过程,这个过程会创建一个临时表。

然后我想从这个表中选择数据,但遇到了错误。

Caught an exception while rendering: (1137, "Can't reopen table: 'temporary_name'")

有没有人能帮我看看我哪里出错了?

我的代码是:

# create a cursor  
cur = connection.cursor()  
# execute the stored procedure   
cur.callproc('nodetree')  

# Get results from stored procedure
sql = "SELECT * FROM temporary_name"

cur.execute(sql)

那个表的名字不是temporary_name,我只是举个例子。

2 个回答

-1

你可以先使用普通的表格,然后再把它删除。

1

来自 MySQL 文档(请注意加粗的部分):

在同一个查询中,你不能多次引用一个临时表。比如,下面的写法是行不通的:

mysql> SELECT * FROM temp_table, temp_table AS t2;
ERROR 1137: Can't reopen table: 'temp_table'

如果你在一个存储函数中使用不同的别名多次引用同一个临时表,也会出现这个错误,即使这些引用是在函数中的不同语句里。

撰写回答