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 6241379
In Process

The Archive Base Latest Questions

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

I’m working on an old database already in use for years and really crappy

  • 0

I’m working on an old database already in use for years and really crappy designed.
There is a table, “Articles”, which contains a “code” column that will be our PK.
And many tables like “idXXXXX” where XXXXX is a “code” value with exactly the same structure.
I looked at the application using this database and saw that relations between tables is made there.

I’m not affraid of redesign the database access in the application, but I don’t want to lose years of entries in the database.

I want to create a “campain” table which will have an “id” PK and a “id_code” as FK linking “campain” to “articles”

I’m not a SQL master but I know I can get tables names with

SELECT TABLE_NAME FROM INFORMATION_SCHEMA WHERE TABLE_NAME LIKE 'id%'

But I have really no idea about how to deal with the result (which is fine).
So how can I access to every tables named “idXXX” and insert every rows in the “campain” table + set “id_code” column to “XXX”?


Here is the procedure I saved (I didn’t add every fields in the INSERT line for testing purpose) :

CREATE PROCEDURE JoinAllTables()
BEGIN

 DECLARE done INT default 0;
 DECLARE tableName CHAR(9); 
 DECLARE buffStr CHAR(7);
 DECLARE buffId INT default 0;

 DECLARE cur1 CURSOR FOR SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME LIKE 'id%'; 
 DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;


 OPEN cur1;

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

  SET buffStr = SUBSTRING(tableName, 3);
  SET buffId = CAST(buffStr AS SIGNED);

  set @sql = CONCAT("INSERT INTO campagnes(id, id_code) SELECT null, bufId FROM ",tableName); # Dynamically building sql statement 
  PREPARE stmt FROM @sql;
  EXECUTE stmt;
  DEALLOCATE PREPARE stmt; 

  END LOOP;
  CLOSE cur1;
END;

As u can see, I sub ‘idXXXXX’ to ‘XXXXX’ then CAST it AS INTEGER (SIGNED).
But I guess that in the “INSERT INTO” line, second tableName doesn’t point to the variable. That’s why I’m getting a
“#1446 – Tabble ‘bddsoufflage.tablename’doesn’t exist” Error 🙂 Any idea ?

  • 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-24T11:45:30+00:00Added an answer on May 24, 2026 at 11:45 am

    Edit: Updated answer

    We can’t have the tableName dynamically changed inside a prepared statement, so we must go through DynamicSQL to build the query using CONCAT, then compile the SQL with PREPARE, EXECUTE it and DEALLOCATE it.

    DELIMITER //
    CREATE PROCEDURE JoinAllTables()
    BEGIN
    
     DECLARE done INT default 0;
     DECLARE tableName CHAR(9); 
     DECLARE buffStr CHAR(7);
     DECLARE buffId INT default 0;
    
     DECLARE cur1 CURSOR FOR SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME LIKE 'id%'; 
     DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
    
     OPEN cur1;
    
     read_loop: LOOP
        FETCH cur1 INTO tableName;
        IF done THEN
           LEAVE read_loop;
        END IF;
    
      SET buffStr = SUBSTRING(tableName, 3);
      SET buffId = CAST(buffStr AS SIGNED);
    
      set @sql = CONCAT("INSERT INTO campagnes(id, id_code) SELECT null, ", buffId, " FROM ",tableName); # Dynamically building sql statement 
      PREPARE stmt FROM @sql;
      EXECUTE stmt;
      DEALLOCATE PREPARE stmt; 
    
      END LOOP;
      CLOSE cur1;
    END; //
    

    See also this answer MySQL Pass table name to cursor select

    Old answer
    The procedure should look something like this. Thanks Mchl for providing an Insert Into query example, I simply added it to the rest of the procedure.

    DELIMITER //
    CREATE PROCEDURE JoinAllTables()
    BEGIN
    
     DECLARE done INT default 0;
     DECLARE tableName CHAR(7); # Variable to contain table names CHAr(7) is assuming id + 5Xs as characters.
    
     DECLARE cur1 CURSOR FOR SELECT TABLE_NAME FROM INFORMATION_SCHEMA WHERE TABLE_NAME LIKE 'id%'; # Create a cursor to iterate over the tables
     DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
    
     OPEN cur1;
    
     read_loop: LOOP
        FETCH cur1 INTO tableName;
        IF done THEN
          LEAVE read_loop;
        END IF;
    
        #Your Insert statement here, using tableName as a field.
    INSERT INTO campain (id, id_code, otherfields) SELECT null, tableName, otherfields FROM tableName;
    
      END LOOP;
      CLOSE cur1;
    END;//
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I want use html5's new tag to play a wav file (currently only supported
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I have a text area in my form which accepts all possible characters from
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka

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.