具有基于Date的动态列的Mysql Prepared语句

时间:2018-02-08 05:55:15

标签: mysql prepared-statement dynamicquery

您好我在存储过程中有一个准备好的语句,根据创建日期动态生成列。问题是当我执行我的存储过程一旦运行良好并运行良好。但是当我改变它的参数值时。它获得错误1054具有未知列示例'十二月'。我读过一些关于它的文章。它说“预准备语句是一种用于以高效率重复执行相同(或类似)SQL语句的功能。”?这是代码。谢谢。请忽略您在编辑时看到的其他参数,不会影响结果。

CREATE DEFINER=`root`@`%` PROCEDURE `MyStoredProcedure`(
IN `_PageIndex` INT, 
`_PageSize` INT, 
 OUT `_RecordCount` INT,
 _DateCreatedFrom VARCHAR(50),
 _DateCreatedTo VARCHAR(50)

) BEGIN

DECLARE _Jan INT;
DECLARE _Feb INT;
DECLARE _Mar INT;
DECLARE _Apr INT;
DECLARE _May INT;
DECLARE _Jun INT;
DECLARE _Jul INT;
DECLARE _Aug INT;
DECLARE _Sep INT;
DECLARE _Oct INT;
DECLARE _Nov INT;
DECLARE _Dec INT;
DECLARE _sql VARCHAR(65535);
DECLARE _sql_dynamic VARCHAR(65535);
DECLARE _GrandTotal VARCHAR(65535);
DECLARE _offset INT;
DECLARE _Year VARCHAR(50);
DROP TEMPORARY TABLE IF EXISTS Results;

SET _Jan = 1;
SET _Feb = 2;
SET _Mar = 3;
SET _Apr = 4;
SET _May = 5;
SET _Jun = 6;
SET _Jul = 7;
SET _Aug = 8;
SET _Sep = 9;
SET _Oct = 10;
SET _Nov = 11;
SET _Dec = 12;
SET _Year = YEAR(CAST(_DateCreatedFrom AS DATE));
SET _DateCreatedFrom = MONTH(CAST(_DateCreatedFrom AS DATE));
SET _DateCreatedTo = MONTH(CAST(_DateCreatedTo AS DATE));
SET _sql = '';
SET _sql_dynamic = '';
SET _GrandTotal = '';
SET @stmt_str = '';


SET _offset = (_PageIndex - 1) * _PageSize;
SET @rownum=0;

SET _sql:= CONCAT('CREATE TEMPORARY TABLE Results ');

SET _sql:= CONCAT(_sql,'SELECT @rownum:=@rownum+1 AS ''#'', a.*, (');


IF(_Jan BETWEEN _DateCreatedFrom AND _DateCreatedTo) THEN
    SET _sql_dynamic:= CONCAT('COUNT(IF(MONTH(tr.`DateCreated`) = 1 AND YEAR(tr.`DateCreated`) = ', _Year ,', 1, null)) AS January,');
    SET _GrandTotal := CONCAT(_GrandTotal,'January + ');
        IF(_DateCreatedTo = 1) THEN
            SET _sql_dynamic:= SUBSTRING(_sql_dynamic,1,LENGTH(_sql_dynamic) - 1);
            SET _GrandTotal := SUBSTRING(_GrandTotal,1,LENGTH(_GrandTotal) - 2);
        END IF;

END IF;

IF(_Feb BETWEEN _DateCreatedFrom AND _DateCreatedTo) THEN
    SET _sql_dynamic:= CONCAT(_sql_dynamic,'COUNT(IF(MONTH(tr.`DateCreated`) = 2 AND YEAR(tr.`DateCreated`) = ', _Year ,', 1, null)) AS February,');
    SET _GrandTotal := CONCAT(_GrandTotal,'February + ');
    IF(_DateCreatedTo = 2) THEN
            SET _sql_dynamic:= SUBSTRING(_sql_dynamic,1,LENGTH(_sql_dynamic) - 2);
            SET _GrandTotal := SUBSTRING(_GrandTotal,1,LENGTH(_GrandTotal) - 2);
    END IF;

END IF;

IF(_Mar BETWEEN _DateCreatedFrom AND _DateCreatedTo) THEN
    SET _sql_dynamic:= CONCAT(_sql_dynamic,'COUNT(IF(MONTH(tr.`DateCreated`) = 3 AND YEAR(tr.`DateCreated`) = ', _Year ,', 1, null)) AS March,');
    SET _GrandTotal := CONCAT(_GrandTotal,'March + ');
    IF(_DateCreatedTo = 3) THEN
            SET _sql_dynamic:= SUBSTRING(_sql_dynamic,1,LENGTH(_sql_dynamic) - 2);
            SET _GrandTotal := SUBSTRING(_GrandTotal,1,LENGTH(_GrandTotal) - 2);
    END IF;

END IF;

IF(_Apr BETWEEN _DateCreatedFrom AND _DateCreatedTo) THEN
    SET _sql_dynamic:= CONCAT(_sql_dynamic,'COUNT(IF(MONTH(tr.`DateCreated`) = 4 AND YEAR(tr.`DateCreated`) = ', _Year ,', 1, null)) AS April,');
    SET _GrandTotal := CONCAT(_GrandTotal,'April + ');
    IF(_DateCreatedTo = 4) THEN
            SET _sql_dynamic:= SUBSTRING(_sql_dynamic,1,LENGTH(_sql_dynamic) - 2);
            SET _GrandTotal := SUBSTRING(_GrandTotal,1,LENGTH(_GrandTotal) - 2);
    END IF;

END IF;

IF(_May BETWEEN _DateCreatedFrom AND _DateCreatedTo) THEN
    SET _sql_dynamic:= CONCAT(_sql_dynamic,'COUNT(IF(MONTH(tr.`DateCreated`) = 5 AND YEAR(tr.`DateCreated`) = ', _Year ,', 1, null)) AS May,');
    SET _GrandTotal := CONCAT(_GrandTotal,'May + ');
    IF(_DateCreatedTo = 5) THEN
            SET _sql_dynamic:= SUBSTRING(_sql_dynamic,1,LENGTH(_sql_dynamic) - 2);
            SET _GrandTotal := SUBSTRING(_GrandTotal,1,LENGTH(_GrandTotal) - 2);
        END IF;

END IF;

IF(_Jun BETWEEN _DateCreatedFrom AND _DateCreatedTo) THEN
    SET _sql_dynamic:= CONCAT(_sql_dynamic,'COUNT(IF(MONTH(tr.`DateCreated`) = 6 AND YEAR(tr.`DateCreated`) = ', _Year ,', 1, null)) AS June,');
    SET _GrandTotal := CONCAT(_GrandTotal,'June + ');
    IF(_DateCreatedTo = 6) THEN
            SET _sql_dynamic:= SUBSTRING(_sql_dynamic,1,LENGTH(_sql_dynamic) - 2);
            SET _GrandTotal := SUBSTRING(_GrandTotal,1,LENGTH(_GrandTotal) - 2);
        END IF;

END IF;

IF(_Jul BETWEEN _DateCreatedFrom AND _DateCreatedTo) THEN
    SET _sql_dynamic:= CONCAT(_sql_dynamic,'COUNT(IF(MONTH(tr.`DateCreated`) = 7 AND YEAR(tr.`DateCreated`) = ', _Year ,', 1, null)) AS July,');
    SET _GrandTotal := CONCAT(_GrandTotal,'July + ');
    IF(_DateCreatedTo = 7) THEN
            SET _sql_dynamic:= SUBSTRING(_sql_dynamic,1,LENGTH(_sql_dynamic) - 2);
            SET _GrandTotal := SUBSTRING(_GrandTotal,1,LENGTH(_GrandTotal) - 2);
        END IF;


END IF;

IF(_Aug BETWEEN _DateCreatedFrom AND _DateCreatedTo) THEN
    SET _sql_dynamic:= CONCAT(_sql_dynamic,'COUNT(IF(MONTH(tr.`DateCreated`) = 8 AND YEAR(tr.`DateCreated`) = ', _Year ,', 1, null)) AS August,');
    SET _GrandTotal := CONCAT(_GrandTotal,'August + ');
    IF(_DateCreatedTo = 8) THEN
            SET _sql_dynamic:= SUBSTRING(_sql_dynamic,1,LENGTH(_sql_dynamic) - 2);
            SET _GrandTotal := SUBSTRING(_GrandTotal,1,LENGTH(_GrandTotal) - 2);
        END IF;

END IF;

IF(_Sep BETWEEN _DateCreatedFrom AND _DateCreatedTo) THEN
    SET _sql_dynamic:= CONCAT(_sql_dynamic,'COUNT(IF(MONTH(tr.`DateCreated`) = 9 AND YEAR(tr.`DateCreated`) = ', _Year ,', 1, null)) AS September,');
    SET _GrandTotal := CONCAT(_GrandTotal,'September + ');
    IF(_DateCreatedTo = 9) THEN
            SET _sql_dynamic:= SUBSTRING(_sql_dynamic,1,LENGTH(_sql_dynamic) - 2);
            SET _GrandTotal := SUBSTRING(_GrandTotal,1,LENGTH(_GrandTotal) - 2);
        END IF;

END IF;

IF(_Oct BETWEEN _DateCreatedFrom AND _DateCreatedTo) THEN
    SET _sql_dynamic:= CONCAT(_sql_dynamic,'COUNT(IF(MONTH(tr.`DateCreated`) = 10 AND YEAR(tr.`DateCreated`) = ', _Year ,', 1, null)) AS October,');
    SET _GrandTotal := CONCAT(_GrandTotal,'October + ');
    IF(_DateCreatedTo = 10) THEN
            SET _sql_dynamic:= SUBSTRING(_sql_dynamic,1,LENGTH(_sql_dynamic) - 2);
            SET _GrandTotal := SUBSTRING(_GrandTotal,1,LENGTH(_GrandTotal) - 2);
        END IF;

END IF;

IF(_Nov BETWEEN _DateCreatedFrom AND _DateCreatedTo) THEN
    SET _sql_dynamic:= CONCAT(_sql_dynamic,'COUNT(IF(MONTH(tr.`DateCreated`) = 11 AND YEAR(tr.`DateCreated`) = ', _Year ,', 1, null)) AS November,');
    SET _GrandTotal := CONCAT(_GrandTotal,'November + ');
    IF(_DateCreatedTo = 11) THEN
            SET _sql_dynamic:= SUBSTRING(_sql_dynamic,1,LENGTH(_sql_dynamic) - 2);
            SET _GrandTotal := SUBSTRING(_GrandTotal,1,LENGTH(_GrandTotal) - 2);
    END IF;

END IF;

IF(_Dec BETWEEN _DateCreatedFrom AND _DateCreatedTo) THEN
    SET _sql_dynamic:= CONCAT(_sql_dynamic,'COUNT(IF(MONTH(tr.`DateCreated`) = 12 AND YEAR(tr.`DateCreated`) = ', _Year ,', 1, null)) AS December ');
    SET _GrandTotal := CONCAT(_GrandTotal,'+ December ');
    IF(_DateCreatedTo = 12) THEN
            SET _sql_dynamic:= SUBSTRING(_sql_dynamic,1,LENGTH(_sql_dynamic) - 2);
            SET _GrandTotal := CONCAT('December ');
        END IF;


END IF;


SET _GrandTotal := CONCAT(_GrandTotal,') ''GRAND TOTAL'' ');
SET _sql := CONCAT(_sql,_GrandTotal);
SET _sql := CONCAT(_sql,'FROM( SELECT IFNULL(Problem, ''GRAND TOTAL'') Problem, ');
SET _sql := CONCAT(_sql,_sql_dynamic, ' FROM trservices  tr
                                  LEFT JOIN libproblems lp on tr.ProblemID=lp.ID
                                  GROUP BY lp.Problem WITH ROLLUP) a;');
SET @stmt_str = _sql;
PREPARE stmt FROM @stmt_str;    
EXECUTE stmt;
DEALLOCATE PREPARE stmt;


SET _RecordCount =(SELECT COUNT(*) FROM Results);

SELECT * FROM Results
LIMIT _offset, _PageSize;

DROP TEMPORARY TABLE Results;

END

继承我输出的图像

Output 1st Run

Output 2nd Run changed TO Date

0 个答案:

没有答案