我有以下存储过程,我从SQL Server迁移,我已经完成了大部分更改,以便在mySQL上运行,但是我在尝试将多个查询结果插入临时表时遇到问题:
-- --------------------------------------------------------------------------------
-- Routine DDL
-- Note: comments before and after the routine body will not be stored by the server
-- --------------------------------------------------------------------------------
DELIMITER $$
CREATE DEFINER=`Test`@`%` PROCEDURE `ListDBATree`()
BEGIN
DECLARE seq int(10);
CREATE TEMPORARY TABLE if not exists DBA_TREE(
ID VARCHAR(10),
NAME VARCHAR(50),
PARENT VARCHAR(30),
VALUE VARCHAR(30),
TYPE varchar(30)
);
insert into DBA_TREE (NAME,VALUE,PARENT,TYPE )
select VIEWNAME, SEQUENCE, null, 'None' from SD_TABLES where HIDDEN = 0;
insert into DBA_TREE (NAME,VALUE,PARENT, TYPE)
select a.DISPLAY_NAME, a.SEQUENCE, (select ID from DBA_TREE where VALUE = a.SEQ_TABLE), a.TYPE from SD_FIELDS a join SD_TABLES b on a.SEQ_TABLE = b.SEQUENCE WHERE b.HIDDEN = 0 and a.ISVIRTUAL = 0;
insert into DBA_TREE (NAME,VALUE,PARENT, TYPE)
select a.DISPLAY_NAME, a.SEQUENCE, (select ID from DBA_TREE where VALUE = a.SEQ_PARENT), a.TYPE from SD_FIELDS a join SD_TABLES b on a.SEQ_TABLE = b.SEQUENCE WHERE b.HIDDEN = 0 and a.ISVIRTUAL = 1;
select * from DBA_TREE;
END
这是错误:
Error Code: 1137. Can't reopen table: 'DBA_TREE'
答案 0 :(得分:2)
正如比尔所说,这是一个已知的错误,我之前做出union
的答案也不会起作用,因为错误说临时表不能被多次引用。例如,你甚至不能像下面那样做,这将导致相同的错误,因为多次引用该表。
select * from DBA_TREE dt1,DBA_TREE dt2 on dt1.id = dt2.id
我之前做过union
的答案也不会有效,因为你实际上不止一次地提到这个表(见下文)
insert into DBA_TREE (NAME,VALUE,PARENT,TYPE )
select VIEWNAME, SEQUENCE, null, 'None' from SD_TABLES where HIDDEN = 0;
union
select a.DISPLAY_NAME, a.SEQUENCE, (select ID from DBA_TREE where VALUE =
..... -- Here Again
我知道这是一个非常尴尬的错误,如果你完全了解错误配置文件,你会发现很多人都面临着严峻的形势,并要求提供修复但是迄今为止都没有引入(甚至没有解决方法)。
因此,在这种情况下,最好的办法是使用普通表而不是临时表。
放弃它
由于您在存储过程中执行所有操作,因此可以执行
CREATE TABLE if not exists DBA_TREE( ID VARCHAR(10), NAME VARCHAR(50), PARENT VARCHAR(30), VALUE VARCHAR(30), TYPE varchar(30) );
insert into DBA_TREE (NAME,VALUE,PARENT,TYPE )
select VIEWNAME, SEQUENCE, null, 'None' from SD_TABLES where HIDDEN = 0;
insert into DBA_TREE (NAME,VALUE,PARENT, TYPE)
select a.DISPLAY_NAME, a.SEQUENCE, (select ID from DBA_TREE where VALUE =
a.SEQ_TABLE), a.TYPE from SD_FIELDS a join SD_TABLES b on a.SEQ_TABLE =
b.SEQUENCE WHERE b.HIDDEN = 0 and a.ISVIRTUAL = 0;
insert into DBA_TREE (NAME,VALUE,PARENT, TYPE)
select a.DISPLAY_NAME, a.SEQUENCE, (select ID from DBA_TREE where VALUE =
a.SEQ_PARENT), a.TYPE from SD_FIELDS a join SD_TABLES b on a.SEQ_TABLE =
b.SEQUENCE WHERE b.HIDDEN = 0 and a.ISVIRTUAL = 1;
select * from DBA_TREE;
然后,在您的存储过程点击END
标记之前,请删除表格
drop table DBA_TREE;