Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7160443
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T13:24:00+00:00 2026-05-28T13:24:00+00:00

SOLVED: This works: DROP PROCEDURE IF EXISTS drop_all_tables_from_specified_database; DELIMITER $$ CREATE PROCEDURE drop_all_tables_from_specified_database(IN dbname

  • 0

SOLVED:

This works:

DROP PROCEDURE IF EXISTS drop_all_tables_from_specified_database;

DELIMITER $$
CREATE PROCEDURE drop_all_tables_from_specified_database(IN dbname CHAR(50))
BEGIN
    DECLARE done INT DEFAULT FALSE;
    DECLARE tblname CHAR(50);
    DECLARE cur1 CURSOR FOR SELECT table_name FROM information_schema.tables WHERE table_schema = dbname COLLATE utf8_general_ci;
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;

    SET foreign_key_checks = 0;

    OPEN cur1;

    read_loop: LOOP
        FETCH cur1 INTO tblname;
        IF done THEN
            LEAVE read_loop;
        END IF;

        SET @database_name = dbname;
        SET @table_name = tblname;
        SET @sql_text = CONCAT('DROP TABLE ', @database_name, '.' , @table_name, ';');      

        PREPARE stmt FROM @sql_text;
        EXECUTE stmt;
        DEALLOCATE PREPARE stmt;

    END LOOP;

    CLOSE cur1;

    SET foreign_key_checks = 1;
END$$
DELIMITER ;

CALL drop_all_tables_from_specified_database('test');

DROP PROCEDURE drop_all_tables_from_specified_database;

I am trying to create a procedure that deletes all tables inside a database. I have saved this code inside a file tear-down.sql:

CREATE PROCEDURE drop_all_tables_from_specified_database()
BEGIN
    DECLARE done INT DEFAULT FALSE;
    DECLARE table_to_drop VARCHAR(255);
    DECLARE cur1 CURSOR FOR SELECT table_name FROM information_schema.tables WHERE table_schema='test';

    OPEN cur1;

    read_loop: LOOP
        FETCH cur1 INTO table_to_drop;
        IF done:
            LEAVE read_loop;
        END IF;
        DROP TABLE `test`.table_to_drop;
    END LOOP;

    CLOSE cur1;
END;

When I then do inside MySQL command line tool:

mysql> source tear-down.sql

I get a massive error:

enter image description here

I don’t understand the error I get and I cannot figure out what’s wrong. Anybody can spot an error in this procedure?

EDIT:

My current code:

DROP PROCEDURE IF EXISTS drop_all_tables_from_specified_database;

DELIMITER $$
CREATE PROCEDURE drop_all_tables_from_specified_database(IN dbname CHAR(50))
BEGIN
    DECLARE done INT DEFAULT FALSE;
    DECLARE tblname CHAR(50);
    DECLARE cur1 CURSOR FOR SELECT table_name FROM information_schema.tables WHERE table_schema = dbname;
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;

    OPEN cur1;

    read_loop: LOOP
        FETCH cur1 INTO tblname;
        IF done THEN
            LEAVE read_loop;
        END IF;
        DROP TABLE dbname.tblname;
    END LOOP;

    CLOSE cur1;
END$$
DELIMITER ;

CALL drop_all_tables_from_specified_database('test');

DROP PROCEDURE drop_all_tables_from_specified_database;

Now it runs ok but it doesn’t delete the tables. I get this response:

enter image description here

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T13:24:00+00:00Added an answer on May 28, 2026 at 1:24 pm

    It looks like you will need to set an alternative DELIMITER:

    DELIMITER $$
    CREATE PROCEDURE drop_all_tables_from_specified_database()
    BEGIN
        DECLARE done INT DEFAULT FALSE;
        DECLARE table_to_drop VARCHAR(255);
        DECLARE cur1 CURSOR FOR SELECT table_name FROM information_schema.tables WHERE table_schema='test';
    
        OPEN cur1;
    
        read_loop: LOOP
            FETCH cur1 INTO table_to_drop;
            /* This might need to be 'IF done <> FALSE THEN' */
            IF done THEN
                LEAVE read_loop;
            END IF;
            DROP TABLE `test`.table_to_drop;
        END LOOP;
    
        CLOSE cur1;
    END$$
    DELIMITER ;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

SOLVED: This is what was wrong: current.addFolder(folder); (in the final else clause of the
I've actually solved this, but I'm posting it for posterity. I ran into a
Edit: I have solved this by myself. See my answer below I have set
I bet somebody has solved this before, but my searches have come up empty.
I know I've solved this problem before, but I can't remember or find the
I have solved this problem, I just need to know what to do. I
I'm betting that someone has already solved this and maybe I'm using the wrong
I'm curious how others have solved this problem, and what problems might lurk behind
In my WPF application I have some drawing functionality. I have solved this using
I understand the problem that OSGI solved thanks to this question.... What does OSGi

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.