Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't UNION ALL on a temporary table?

Tags:

sql

mysql

I'm trying to run the following simple test- creating a temp table, and then UNIONing two different selections:

CREATE TEMPORARY TABLE tmp 
SELECT * FROM people;

SELECT * FROM tmp
UNION ALL
SELECT * FROM tmp;

But get a #1137 - Can't reopen table: 'tmp'

I thought temp tables were supposed to last the session. What's the problem here?

like image 443
Yarin Avatar asked Aug 25 '12 12:08

Yarin


People also ask

Do you have to manually delete temporary tables?

The use of temporary tables, or temp tables in SQL terms, is common in SQL, but once we're done with those tables, they should be deleted, or dropped. Using the DROP TABLE command on a temporary table, as with any table, will delete the table and remove all data.

Can temporary table have trigger?

You cannot associate a trigger with a TEMPORARY table or a view. Trigger names exist in the schema namespace, meaning that all triggers must have unique names within a schema. Triggers in different schemas can have the same name.

What can I use instead of a temp table?

A valuable alternatives for the SQL temp table and table variable are SCHEMA_ONLY Memory-Optimized tables and the Memory-optimized Table Variable, where the data will be completely stored in the memory without the need to touch the TempDB database, providing the best data access performance.

Are temp tables dropped automatically?

If the session where the global temporary table was created is closed, the global temporary table will be dropped automatically. Names start with a single “#” hashtag symbol. Names start with a double “##” hashtag symbol. Tables can be accessed only from the session where the table was created.


3 Answers

This error indicates that the way in which MySQL tables manages the temporary tables has been changed which in turn affects the joins, unions as well as subqueries. To fix MySQL error "can’t reopen table", try out the following solution:

mysql> CREATE TEMPORARY TABLE tmp_journals_2 LIKE tmp_journals;

Query OK, 0 rows affected (0.00 sec)

mysql> INSERT INTO tmp_journals_2 SELECT * FROM tmp_journals;

After this you can perform the union operation.


Useful reading

  • http://dev.mysql.com/doc/refman/5.0/en/temporary-table-problems.html
  • http://www.mysqlrepair.org/mysqlrepair/cant-reopen-table.php
like image 54
heretolearn Avatar answered Oct 06 '22 02:10

heretolearn


Figured it out thanks to sshekar's answer- the solution in this case would be

  1. Create an empty temp table
  2. Insert the results we want to UNION into the table separately
  3. Query the temp table

SQL:

CREATE TEMPORARY TABLE tmp LIKE people;

INSERT INTO tmp SELECT * FROM people; /* First half of UNION */
INSERT INTO tmp SELECT * FROM people; /* Second half of UNION */
SELECT * FROM tmp; 

(See Using MySQL Temporary Tables to save your brain)

like image 34
Yarin Avatar answered Oct 06 '22 03:10

Yarin


As documented under TEMPORARY Table Problems:

You cannot refer to a TEMPORARY table more than once in the same query. For example, the following does not work:

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

This error also occurs if you refer to a temporary table multiple times in a stored function under different aliases, even if the references occur in different statements within the function.

like image 45
eggyal Avatar answered Oct 06 '22 03:10

eggyal