MySQL:在CONCAT INSERT INTO SELECT

时间:2018-05-29 11:32:26

标签: mysql sql local-variables

我只想创建一个创建一个带有变量名的表的过程,并插入我从另一个表中选择的东西。然而,表的创建工作,但我在以下代码中尝试包含时总是收到错误消息。

它始终说我在部分中有某种语法错误,我用参数中的变量检查提供日期。

我希望你们能理解我并帮助我。

DELIMITER //
CREATE OR REPLACE PROCEDURE Prov (start_d DATE, end_d DATE, month_name CHAR(20))
BEGIN   



    SET @table := month_name;
    SET @start_d = start_d;
    SET @end_d = end_d;

    SET @sql_text1 := CONCAT('
        CREATE TABLE ',@table,'(
        provision_ID INT NOT NULL AUTO_INCREMENT PRIMARY KEY, 
        name VARCHAR(50) NOT NULL, 
        provision float NOT NULL)'
    ); 

    PREPARE stmt from @sql_text1; 
    EXECUTE stmt; 


    SET @sql_text2 := CONCAT('
    INSERT INTO', @table, ' 
    (
        SELECT Unternehmen.unternehmen_ID, Unternehmen.name, 
        SUM(Jobangebot.dauerStunden * Jobangebot.bezahlung * 0.02) AS Provision 
        FROM unternehmen 
        INNER JOIN Jobangebot ON Unternehmen_ID = unternehmen_ID_FK 
        WHERE status = ''angenommen'' OR status = ''offen'' 
        AND dateOfOffer >=, ' @start_d,' AND dateOfOffer <=', @end_d,' 
        GROUP BY unternehmen.unternehmen_ID
    )');

    PREPARE stmt from @sql_text2; 
    EXECUTE stmt; 

END
 //

DELIMITER ;


call Prov('2018-05-01','2018-05-31', 'March');

1 个答案:

答案 0 :(得分:0)

您将不得不将日期视为文字(将其包装在引号中)。

[...]
WHERE status = ''angenommen'' OR status = ''offen'' 
AND dateOfOffer >= "',@start_d,'" AND dateOfOffer <= "',@end_d,'"
GROUP BY unternehmen.unternehmen_ID
[...]