Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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. 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. 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. 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. Ever.

Dev Server: mySQL Server version: 5.1.47-community Prod Server: mySQL Server version: 5.0.45-log

We can't change db versions (DBAs won't allow it), so I'm stuck on what I have. Is this a bug in mySQL or in the Procedure?

Thanks.

like image 549
Coach John Avatar asked Sep 01 '10 17:09

Coach John


People also ask

Can we DROP TABLE from stored procedure?

You can use these scripts to drop database, table, or other elements In the beginning of your stored procedure, and then create them freely during the SP. While recreating the table you have full control over the data types of the table columns (fields is a wrong word by the way in this context).

How do you drop a stored procedure if it exists in SQL Server?

To drop the procedure, we have to write a conditional statement to check if the store procedure exists or not then write the drop statement. Otherwise, it will raise an error in case the stored procedure does not exist.

What is the significance of if exists clause while dropping a table?

IF EXISTS clause: The optional IF EXISTS clause makes the statement succeed whether or not the table exists. If the table does exist, it is dropped; if it does not exist, the statement has no effect. This capability is useful in standardized setup scripts that remove existing schema objects and create new ones.

Does DROP TABLE lock MySQL?

Yes, select . So, if you loop over a cursor in mysql (or php, for example with pdo::fetch ), and you run a ddl statement on the same table(s), you will get a deadlock.


1 Answers

It's an old question but it came up as I was looking for DROP TABLE IF EXISTS.

Your non-working code did not work on my MySQL 5.1.70 server.

All I had to do was add a space between DELIMITER and // on the first line, and everything worked fine.

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 ;
like image 134
IvanD Avatar answered Sep 19 '22 13:09

IvanD