简体   繁体   English

如何在MySQL存储过程中使用DROP TABLE IF EXISTS

[英]How to use DROP TABLE IF EXISTS in a MySQL Stored Procedure

I want to know how to use DROP TABLE IF EXISTS in a MySQLstored procedure. 我想知道如何在MySQL存储过程中使用DROP TABLE IF EXISTS I'm writing a rather long mySQL Stored Procedure that will do a bunch of work and then load up a temp table with the results. 我正在编写一个相当长的mySQL存储过程,它将执行大量工作,然后加载带有结果的临时表。 However, I am having trouble making this work. 但是,我无法完成这项工作。

I've seen a few ways to do the temp table thing. 我已经看到了一些方法来做临时表的事情。 Basically, you either create the temp table, work on it, and then drop it at the end ... or you drop it if it exists, create it, and then do your work on it. 基本上,您要么创建临时表,要对其进行处理,然后将其放在最后...或者如果它存在则将其删除,创建它,然后对其进行处理。

I prefer the second method so that you always start of clean, and it's a built-in check for the table's existence. 我更喜欢第二种方法,所以你总是开始干净,这是对表存在的内置检查。 However, I can't seem to get it to work: 但是,我似乎无法让它工作:

Here are my examples: 这是我的例子:

This Works: 这个作品:

DELIMITER//
    DROP PROCEDURE IF EXISTS pTest//
    CREATE PROCEDURE pTest()
    BEGIN
        CREATE TEMPORARY TABLE tblTest (
            OrderDate varchar(200)
        );
        DROP TEMPORARY TABLE tblTest;
    END//
 DELIMITER ;
CALL pTest();

This Works: 这个作品:

DELIMITER//
    DROP PROCEDURE IF EXISTS pTest//
    CREATE PROCEDURE pTest()
    BEGIN
        DROP TEMPORARY TABLE tblTest;
        CREATE TEMPORARY TABLE tblTest (
            OrderDate varchar(200)
        );
    END//
 DELIMITER ;
CALL pTest();

This does not: 这不是:

DELIMITER//
    DROP PROCEDURE IF EXISTS pTest//
    CREATE PROCEDURE pTest()
    BEGIN
        DROP TEMPORARY TABLE IF EXISTS tblTest;
        CREATE TEMPORARY TABLE tblTest (
            OrderDate varchar(200)
        );
    END//
 DELIMITER ;
CALL pTest();

The first 2 work, but if that table exists (like if the procedure didn't finish or something), it'll obviously end with a "Table tblTest does not exist" error. 前两个工作,但如果该表存在(如果该过程没有完成或某事),它显然会以“表tblTest不存在”错误结束。 The non-working example is what I'm looking for -- drop the table if its there and then recreate it so that I can start clean. 我正在寻找的非工作示例 - 如果它在那里删除表,然后重新创建它,以便我可以开始清理。

It feels like it's the "IF EXISTS" making this thing fail. 感觉就像是“IF EXISTS”让这件事失败了。 I've copied code from all sorts of sites that do things very similar and in no case can I get a "DROP TABLE IF EXISTS..." to work. 我从各种非常相似的网站复制了代码,但在任何情况下我都无法获得“DROP TABLE IF EXISTS ...”。 Ever. 永远。

Dev Server: mySQL Server version: 5.1.47-community Prod Server: mySQL Server version: 5.0.45-log 开发服务器:mySQL服务器版本:5.1.47-community Prod服务器:mySQL服务器版本:5.0.45-log

We can't change db versions (DBAs won't allow it), so I'm stuck on what I have. 我们无法更改数据库版本(DBA不会允许它),所以我坚持我所拥有的。 Is this a bug in mySQL or in the Procedure? 这是mySQL或程序中的错误吗?

Thanks. 谢谢。

It's an old question but it came up as I was looking for DROP TABLE IF EXISTS. 这是一个老问题,但它出现了,因为我正在寻找DROP TABLE IF EXISTS。

Your non-working code did not work on my MySQL 5.1.70 server. 您的非工作代码无法在我的MySQL 5.1.70服务器上运行。

All I had to do was add a space between DELIMITER and // on the first line, and everything worked fine. 我所要做的就是在第一行的DELIMITER和//之间添加一个空格,一切正常。

Working code: 工作代码:

DELIMITER //
    DROP PROCEDURE IF EXISTS pTest//
    CREATE PROCEDURE pTest()
    BEGIN
        DROP TEMPORARY TABLE IF EXISTS tblTest;
        CREATE TEMPORARY TABLE tblTest (
            OrderDate varchar(200)
        );
    END//
DELIMITER ;

I also had the same problem. 我也有同样的问题。 It seems MySQL doesn't like to check if the table exists on some versions or something. 似乎MySQL不喜欢检查表是否存在于某些版本或某些版本上。 I worked around the issue by querying the database first, and if I found a table I dropped it. 我首先通过查询数据库解决了这个问题,如果我找到了一个表,我就把它丢弃了。 Using PHP: 使用PHP:

$q = @mysql_query("SELECT * FROM `$name`");
if ($q){
    $q = mysql_query("DROP TABLE `$name`");
    if(!$q) die('e: Could not drop the table '.mysql_error());
}

You suppress the error in the first query with the @ symbol, so you don't have an interfering error, and then drop the table when the query returns false. 您使用@符号抑制第一个查询中的错误,因此您没有干扰错误,然后在查询返回false时删除该表。

I don't know why this is not working for you,but you should be able to work around the issue using a continue handler. 我不知道为什么这不适合你,但你应该能够使用continue处理程序来解决这个问题。 If you put the DROP TABLE statement into it's own BEGIN...END block you can use a continue handler to ignore the error if the table does not exist. 如果将DROP TABLE语句放入其自己的BEGIN...END块,则可以使用continue处理程序在表不存在时忽略该错误。

Try this: 尝试这个:

DELIMITER //
    DROP PROCEDURE IF EXISTS pTest //
    CREATE PROCEDURE pTest()
    BEGIN
      BEGIN
        DECLARE CONTINUE HANDLER FOR SQLSTATE '42S02' BEGIN END;
        DROP TEMPORARY TABLE tblTest;
      END;
        CREATE TEMPORARY TABLE tblTest (
            OrderDate varchar(200)
        );
    END //
 DELIMITER ;
CALL pTest();

I recommend to add new line 我建议添加新行

SET sql_notes = 0// before DROP PROCEDURE IF EXISTS get_table //

Otherwise it will show warning PROCEDURE does not exists. 否则会显示警告PROCEDURE不存在。

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

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