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

  • SEARCH
  • Home
  • 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 605979
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T17:12:18+00:00 2026-05-13T17:12:18+00:00

I have been trying to make a stored procedure which autodeletes temporary tables. CREATE

  • 0

I have been trying to make a stored procedure which autodeletes temporary tables.

CREATE PROCEDURE DeleteTemp()
BEGIN
DECLARE no_more_rows BOOLEAN;
DECLARE loop_cntr INT DEFAULT 0;
DECLARE num_rows INT DEFAULT 0;


DECLARE tmptablename VARCHAR(100);
DECLARE tmpTables CURSOR FOR
    SELECT TABLE_NAME
    FROM information_schema.TABLES
    WHERE TABLE_SCHEMA='myDB' AND TABLE_NAME LIKE 'tmp%';
DECLARE CONTINUE HANDLER FOR NOT FOUND SET no_more_rows = TRUE;
OPEN tmpTables;
SELECT FOUND_ROWS() into num_rows;
the_loop: LOOP
    FETCH tmpTables INTO tmptablename;
    DROP TABLE tmptablename;
    IF no_more_rows THEN
        CLOSE tmpTables;
        LEAVE the_loop;
    END IF;
END LOOP the_loop;
END

However, all I get is:

Query : call DeleteTemp
Error Code : 1051
Unknown table ‘tmptablename’

How can I pass the variable tmptablename properly into the “DROP TABLE” command?


Fixed using prepared statements.

CREATE
    PROCEDURE DeleteTemp()
    BEGIN
    DECLARE no_more_rows BOOLEAN;
    DECLARE loop_cntr INT DEFAULT 0;
    DECLARE num_rows INT DEFAULT 0;


    DECLARE tmptablename VARCHAR(100);
    DECLARE tmpTables CURSOR FOR
        SELECT TABLE_NAME
        FROM information_schema.TABLES
        WHERE TABLE_SCHEMA='MY_SCHEMA'
            AND TABLE_NAME LIKE 'tmp%';
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET no_more_rows = TRUE;
    OPEN tmpTables;
    SELECT FOUND_ROWS() INTO num_rows;

    the_loop: LOOP
        FETCH tmpTables INTO tmptablename;
        IF no_more_rows THEN
            CLOSE tmpTables;
            LEAVE the_loop;
        ELSE
            SET @a:=CONCAT('DROP TABLE ',tmptablename);
            PREPARE stmt1 FROM @a;
            EXECUTE stmt1;
            DEALLOCATE PREPARE stmt1;
        END IF;
    END LOOP the_loop;
    END
  • 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-13T17:12:19+00:00Added an answer on May 13, 2026 at 5:12 pm

    Have you had a look at the PREPARE and EXECUTE commands. This is like executing DYNAMIC SQL

    SQL Syntax for Prepared Statements

    The following SQL statements can be
    used in prepared statements: ALTER
    TABLE, CALL, COMMIT, CREATE INDEX,
    CREATE TABLE, DELETE, DO, DROP INDEX,
    DROP TABLE, INSERT, RENAME TABLE,
    REPLACE, SELECT, SET, UPDATE, and most
    SHOW statements. ANALYZE TABLE,
    OPTIMIZE TABLE, and REPAIR TABLE are
    also supported as of MySQL 5.0.23.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been trying to find ways to make the following piece of code
I have been building applications / web apps for some time, and I guess
I have having some trouble printing a char from an ASCII value stored in
We have several ASP.NET applications that use a sitemap which is populated via a
I am trying to create a UITableView where more rows loads automatically as I
Guys, I have got a slightly different problem with Java Dynamic class loading. I
I'm trying to parse an html output file from a program called mirdeep. I
I have a Server communicating with multiple clients through a socket connection. In my
I'm trying to learn git by applying it (retroactively) to a project where I
I am in the sitaution where I need to use an external session state

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.