是否可以从TEMPTABLE读取数据并将其写入另一个TEMPTABLE?

时间:2016-10-21 08:08:16

标签: mysql sql database stored-procedures

我需要知道是否可以从 temptable 中读取日期,并再次使用MySQL中的存储过程将该数据插入另一个 temptable

我想从一个临时表中读取数据并再次按照我希望插入的顺序将该数据插入另一个临时表中吗?

  

temptable1(阅读)--------> temptable2(写)

提前致谢

这是我的存储过程

DELIMITER //
create procedure search(
                                in search_key varchar(255)
                            ) 
BEGIN 

    -- Drop the temp table if already exist

    DROP temporary table IF EXISTS temptable; 

    -- Creating temp table 

    create temporary table temptable( 

        SELECT    member .member_type

              FROM       member member 
              LEFT JOIN org_person orgPerson 
              ON         orgPerson.person_id = member .id 
              INNER JOIN organization organization 
              ON         organization.id = orgPerson.org_id  
              ORDER BY   organization.name);

    -- Drop the temp table if already exist

    DROP temporary table IF EXISTS temptableSearch;

    -- Creating another temp table

    create temporary table temptableSearch like temptable;

    -- Inserting value from table

     insert into temptableSearch 
                  select *
                  from   temptable temp
                  where  temp.name like 'search_key' 

            UNION

                  select *
                  from   temptable temp
                  where  temp.name like 'search_key%' 

            UNION

                  select * 
                  from   temptable  temp 
                  where  temp.name like '%search_key%'; 

    select DISTINCT * 
  from            temptableSearch;

  END // 
  DELIMITER ;

当我调用它时,它会显示以下错误

Error Code: 1137. Can't reopen table: 'temp'

我发现有一件事因为这部分而发生错误

insert into temptableAffiliationSearch 
                      select *
                      from   temptableAffiliation temp
                      where  temp.name like 'search_key' 

                UNION

                      select *
                      from   temptableAffiliation temp
                      where  temp.name like 'search_key%' 

                UNION

                      select * 
                      from   temptableAffiliation temp 
                      where  temp.name like '%search_key%';

1 个答案:

答案 0 :(得分:0)

我找到了答案

正如我之前所说,错误发生在这部分

        insert into temptableSearch 
                  select *
                  from   temptable temp
                  where  temp.name like 'search_key' 

            UNION

                  select *
                  from   temptable temp
                  where  temp.name like 'search_key%' 

            UNION

                  select * 
                  from   temptable temp 
                  where  temp.name like '%search_key%'; 
  

我们无法在同一查询中多次访问临时表

所以我将查询重写为

   insert into temptableSearch 
                  select *
                  from   temptable temp
                  where  temp.name like 'search_key' ;

   insert into temptableSearch 

                  select *
                  from   temptable temp
                  where  temp.name like 'search_key%' ;

   insert into temptableSearch 

                  select * 
                  from   temptable temp 
                  where  temp.name like '%search_key%'; 

现在它工作正常。