简体   繁体   中英

Temporary Table in Stored Procedure Not Found in MySQL

I've inherited some code that uses stored procedures and one of them doesn't appear to be working correctly.

The stored procedure uses a temporary table to insert data later in the procedure. However when I execute the stored procedure, no data is inserted. When I debug it, I get the error:

Table 'db.testtable' doesn't exist

I've stripped down the stored procedure to the following code, and it doesn't work. I always get the error on the SELECT statement. Everything looks OK from what I can tell based on the examples I've seen.

DROP PROCEDURE IF EXISTS db.insert_record;
CREATE PROCEDURE db.`insert_record`(id int, status int)
BEGIN
    DECLARE code varchar(45);

    DROP TEMPORARY TABLE IF EXISTS testTable;

    CREATE TEMPORARY TABLE testTable AS (SELECT 'TEST' AS fakeColumn);

    SELECT fakeColumn INTO code FROM testTable;
END;

I've also verified that the user I am connected as has the permission to create temporary tables; in fact it has every permission available

Additional Details

Running MySQL 5.6 on Windows.

If I take the drop / create / select statements by themselves and run as a script, it behaves as expected.

Using Toad for MySQL to debug the stored procedure.

I can't reproduce the problem. What other information can you provide?

mysql> USE `test`;
Database changed

mysql> SELECT VERSION();
+-----------------+
| VERSION()       |
+-----------------+
| 5.5.22-0ubuntu1 |
+-----------------+
1 row in set (0.00 sec)

mysql> DELIMITER //

mysql> DROP PROCEDURE IF EXISTS `insert_record`//
Query OK, 0 rows affected (0.00 sec)

mysql> CREATE PROCEDURE `insert_record`(`id` int, `status` int)
    -> BEGIN
    ->   DECLARE `code` VARCHAR(45);
    ->   DROP TEMPORARY TABLE IF EXISTS `testTable`;
    ->   CREATE TEMPORARY TABLE `testTable` AS (SELECT 'TEST' AS `fakeColumn`);
    ->   SELECT `fakeColumn` INTO `code` FROM `testTable`;
    ->   SELECT `code`;
    -> END//
Query OK, 0 rows affected (0.01 sec)

mysql> CALL `insert_record`(NULL, NULL)//
+--------+
| `code` |
+--------+
| TEST   |
+--------+
1 row in set (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

SQL Fiddle demo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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