简体   繁体   English

Mysql存储过程:无法重新打开表:“临时表”。 如何解决这个问题

[英]Mysql stored procedure: Can't reopen table: 'temporary table'. How to solve this issue

I've successful execute this procedure when I call the stored procedure, I get error message: 调用存储过程时,我已成功执行了此过程,并收到错误消息:

Can't reopen table: 'temp1'. 无法重新打开表:“ temp1”。

Its like temporary table used more than once in the query. 它像临时表在查询中多次使用。 Any ideas or other solution to solve this problem ? 有什么想法或其他解决方案来解决这个问题吗?

DELIMITER //
CREATE PROCEDURE menu_t1()
BEGIN
CREATE TEMPORARY TABLE if not exists temp1(
    object_id VARCHAR(50) NULL, 
    description VARCHAR(100) NULL,
    display_name VARCHAR(60) NULL,
    menu_id VARCHAR(40) NULL,
    order_seq int NULL,
    menu_level int NULL,
    link_url VARCHAR(200) NULL,
    OrderString VARCHAR(200) NULL
    );

    insert into temp1 (object_id, description, display_name, menu_id, order_seq, menu_level, link_url, OrderString)
    SELECT mp.object_id, mp.description, mp.display_name, mp.menu_id, mp.order_seq, mp.menu_level, mp.link_url,
    concat('00',cast((mp.order_seq) as char)) OrderString 
    FROM ui_menuitem mp 
    WHERE mp.parent_id is null and mp.menu_id = '1';

    select * from temp1;

   select * from temp1
   union all
   select mc.object_id, mc.description, mc.display_name, mc.menu_id, mc.order_seq, mc.menu_level, mc.link_url, 
    concat(temp1.OrderString,'-',cast((mc.order_seq) as char)) OrderString
    from ui_menuitem mc, temp1
    where mc.parent_id = temp1.object_id and mc.menu_id = '1';

    TRUNCATE TABLE temp1;
   DROP TEMPORARY TABLE IF EXISTS temp1;

COMMIT;
END //
DELIMITER ;

This style looks like recursive. 这种样式看起来像递归的。

Thanks 谢谢

While you have declared the temporary table, you've not defined anything about it, such as columns, types, nor inserted any data into it. 声明临时表后,您尚未定义任何临时表,例如列,类型,也未在其中插入任何数据。 All you've done is declare it, and then perform a select statement. 您要做的就是声明它,然后执行一条select语句。

You need to instead define your table by doing something like this 您需要通过执行以下操作来定义表

CREATE TABLE #temp1(
    object_id INT, 
    description VARCHAR(200)
    ...)

And then insert the data into it like this 然后像这样将数据插入其中

INSERT INTO #temp1(object_id, description, ...)
SELECT mp.object_id, mp.description, ...

You are getting the error at present as temp1 currently has no OrderString column. 由于temp1当前没有OrderString列,因此您正在收到错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM