简体   繁体   English

在MySQL中找不到存储过程中的临时表

[英]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. 我总是在SELECT语句上收到错误。 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. 在Windows上运行MySQL 5.6。

If I take the drop / create / select statements by themselves and run as a script, it behaves as expected. 如果我自己执行drop / create / select语句并作为脚本运行,它的行为将与预期的一样。

Using Toad for MySQL to debug the stored procedure. 使用Toad for MySQL调试存储过程。

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 SQL Fiddle演示

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

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