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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T15:03:10+00:00 2026-06-12T15:03:10+00:00

This has me seriously stumped. I have a MySQL stored procedure that looks like

  • 0

This has me seriously stumped. I have a MySQL stored procedure that looks like this.

 DELIMITER $$

CREATE PROCEDURE `lms`.`leads_to_bak` ()
BEGIN
SET @table1 = (SELECT `tabler_name` FROM `sets` WHERE `on_off`=0 LIMIT 1);
SET @table2 = CONCAT(@table1, '_bak');
SET @SQL1 = CONCAT('INSERT INTO ',@table2, '(', (SELECT REPLACE(GROUP_CONCAT(COLUMN_NAME), 'lead_id,', '') FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = @table2), ')', ' SELECT ', (SELECT REPLACE(GROUP_CONCAT(COLUMN_NAME), 'lead_id,', '') FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = @table1), ' FROM ', @table1);
PREPARE stmt FROM @sql1;
EXECUTE stmt;
END$$

DELIMITER ;

Now the part that is important is this:

SET @SQL1 = CONCAT('INSERT INTO ',@table2, '(', (SELECT REPLACE(GROUP_CONCAT(COLUMN_NAME), 'lead_id,', '') FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = @table2), ')', ' SELECT ', (SELECT REPLACE(GROUP_CONCAT(COLUMN_NAME), 'lead_id,', '') FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = @table1), ' FROM ', @table1);

Now from the looks of it, the statement can be translated to

INSERT INTO @table2 (col1, col2, col3, ...)[minus lead_id] SELECT (col1, col2, col3, ...)[minus lead_id] FROM @table1

Pretty simple right? Well I am trying to attach a WHERE clause at the end the end of that there INSERT statement to make it look something like this:

INSERT INTO @table2 (col1, col2, col3, ...) SELECT (col1, col2, col3, ...) FROM @table1 WHERE set_id = (SELECT set_id FROM sets WHERE on_off = 0);

But no dice. Errors galore. Heck I even tried:

INSERT INTO (col1, col2, col3, ...) SELECT (col1, col2, col3, ...) FROM @table1 WHERE set_id = 1;

The string output is:

INSERT INTO leads_auto_bak(set_id,new_old,upload_date,agent,vendors,callcenter,generation_date,first_name,last_name,email,phone,address,city,state,zip,dob,gender,marital_status,rented,year,make,model,trim,vin,primary_use,miles_oneway,mileage,license_num,license_state,education,job_title,license_status) SELECT set_id,new_old,upload_date,agent,vendors,callcenter,generation_date,first_name,last_name,email,phone,address,city,state,zip,dob,gender,marital_status,rented,year,make,model,trim,vin,primary_use,miles_oneway,mileage,license_num,license_state,education,job_title,license_status FROM leads_auto

But still nothing. I played around with the different ways to CONCAT that line at the end but it wont budge. Anyone got some advise?

  • 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-06-12T15:03:11+00:00Added an answer on June 12, 2026 at 3:03 pm

    You are missing a table name to insert into:

    INSERT INTO MYTABLE (col1, col2, col3) 
    SELECT col1, col2, col3
    FROM @table1 
    WHERE ...
    

    Also, notice there should be no brackets around the columns in your SELECT clause.

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

Sidebar

Related Questions

This has defeated me. I want to have a static class variable which is
This has been a rather problematic issue on numerous occasions. We have alot of
This one really has me. I have this validation in my user model: validates
Hi this is something that's really bothering me and I'm hoping someone has an
I'm seriously about to stop coding and become a carpenter. This problem has had
Let's say that i've got a model. This model has data for position, color,
I have been reading this post on the codeigniter forum, http://codeigniter.com/forums/viewthread/174928/ and it has
I have a problem that I seriously spent months on now! Essentially I am
We have an application that has multiple identical databases apart from customer customizations. we
I'm dealing with an issue with my current employer that has seriously made me

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.