MySql准备语句缓存

时间:2016-02-29 08:43:02

标签: mysql

我写了一个包含动态日期列的SP,为实现这个我已经使用了PREPARED语句。当我改变开始和结束日期时,它会给出错误。

任何人都可以帮助我。

SET @dynQuery = CONCAT("CREATE TEMPORARY TABLE temp_info
                            SELECT
                                exitStat.department_id AS department_id
                                ,exitStat.cadre_type_id AS cadre_id
                                ,dept.name AS department_name
                                ,lkup.lkup_name
                                ,'TEMP' AS row_type
                                ,0 AS lvl
                                ,0 AS children");   

WHILE(fromMon <= toMon) DO
    SET dateColName = REPLACE(fromMon, "-", "_");

    SET @dynQuery = CONCAT(@dynQuery, 
                        ", exitStat.", dateColName , "_", joiners,
                        ", exitStat.", dateColName , "_", exits                         
                    );
    SET fromMon = DATE_ADD(fromMon, INTERVAL 1 MONTH);
END WHILE;

SET @dynQuery = CONCAT(@dynQuery, " FROM exit_statistics exitStat
                            JOIN lkup_types lkup ON lkup.id = exitStat.cadre_type_id
                            JOIN departments dept ON dept.id = exitStat.department_id");

IF departmentId > 0 AND cadreTypeId > 0 THEN
    SET @dynQuery = CONCAT(@dynQuery, " WHERE exitStat.department_id IN (", departmentId ,") AND exitStat.cadre_type_id IN (", cadreTypeId, ")");
ELSEIF departmentId > 0 THEN 
    SET @dynQuery = CONCAT(@dynQuery, " WHERE exitStat.department_id IN (", departmentId, ")");
ELSEIF cadreTypeId > 0 THEN
    SET @dynQuery = CONCAT(@dynQuery, " WHERE exitStat.cadre_type_id IN (", cadreTypeId, ")");      
END IF;


PREPARE stmt FROM @dynQuery;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

0 个答案:

没有答案