Cela m'a sérieusement perplexe. J'ai une procédure stockée MySQL qui ressemble à ceci.
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 ;
Maintenant, la partie qui est importante est la suivante:
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);
Maintenant, à première vue, la déclaration peut être traduite en
INSERT INTO @table2 (col1, col2, col3, ...)[minus lead_id] SELECT (col1, col2, col3, ...)[minus lead_id] FROM @table1
Assez simple, non? Eh bien, j'essaie de joindre un WHERE
clause à la fin de la fin de là INSERT
pour le faire ressembler à ceci:
INSERT INTO @table2 (col1, col2, col3, ...) SELECT (col1, col2, col3, ...) FROM @table1 WHERE set_id = (SELECT set_id FROM sets WHERE on_off = 0);
Mais pas de dés. Des erreurs à gogo. Zut j'ai même essayé:
INSERT INTO (col1, col2, col3, ...) SELECT (col1, col2, col3, ...) FROM @table1 WHERE set_id = 1;
La sortie de chaîne est:
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
Mais toujours rien. J'ai joué avec les différentes façons de CONCAT
cette ligne à la fin, mais elle ne bougera pas. Quelqu'un a-t-il des conseils?
Réponses:
0 pour la réponse № 1Il vous manque un nom de table à insérer dans:
INSERT INTO MYTABLE (col1, col2, col3)
SELECT col1, col2, col3
FROM @table1
WHERE ...
Notez également qu'il ne devrait pas y avoir de parenthèses autour des colonnes de votre SELECT
clause.