Bug #27566 | INSERT ... SELECT: Can't insert in the table in FROM in stored procedure | ||
---|---|---|---|
Submitted: | 31 Mar 2007 19:37 | Modified: | 3 Aug 2007 17:32 |
Reporter: | Ondra Zizka | Email Updates: | |
Status: | Not a Bug | Impact on me: | |
Category: | MySQL Server: Documentation | Severity: | S3 (Non-critical) |
Version: | 5.0.37 | OS: | Windows (Win32) |
Assigned to: | Paul DuBois | CPU Architecture: | Any |
[31 Mar 2007 19:37]
Ondra Zizka
[1 Apr 2007 17:28]
Valeriy Kravchuk
Thank you for a bug report. Verified just as described.
[3 Aug 2007 15:11]
Konstantin Osipov
This is not a bug. MySQL manual says: http://dev.mysql.com/doc/refman/5.0/en/temporary-table-problems.html: # 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' When manual talks about being able to INSERT ... SELECT into the same table, it describes base, not temporary tables. The limitation of not being able to re-open a temporary table still applies. Note, that the problematic code produces the same error outside a stored procedure: mysql> CREATE TEMPORARY TABLE tree_subnodes ( -> id INT UNSIGNED NOT NULL PRIMARY KEY, -> level SMALLINT UNSIGNED -> ); Query OK, 0 rows affected (0.00 sec) mysql> INSERT INTO tree_subnodes -> SELECT t.id, iCurLevel+1 -> FROM tree_subnodes AS ts -> INNER JOIN fstree AS t ON t.pod = ts.id -> WHERE ts.level = iCurLevel -- filtrace aktualni vrstvy -> AND t.level = iCurLevel+1; ERROR 1137 (HY000): Can't reopen table: 'tree_subnodes' Suggestion for documentation: please clarify that INSERT ... SELECT still cannot INSERT ... SELECT into the same *temporary* table. Thank you.
[3 Aug 2007 17:32]
Paul DuBois
I have updated the INSERT ... SELECT section to point out that TEMPORARY tables cannot be used: The target table of the INSERT statement may appear in the FROM clause of the SELECT part of the query. (This was not possible in some older versions of MySQL.) In this case, MySQL creates a temporary table to hold the rows from the SELECT and then inserts those rows into the target table. However, it remains true that you cannot use INSERT INTO t ... SELECT ... FROM t when t is a TEMPORARY table, because TEMPORARY tables cannot be referred to twice in the same statement.