如何在存储过程中使用预准备语句的结果

时间:2014-04-15 14:09:15

标签: php mysql stored-procedures mysqli prepared-statement

我在Mysql中创建了一个存储过程,其中我使用下面准备好的语句从一个动态命名表中获取计数并尝试在@a中获取结果计数但在运行其显示错误时。

错误错误代码:1064 您的SQL语法有错误;检查与MySQL服务器版本对应的手册,以便在第1行的“NULL”附近使用正确的语法

    SET prevDate=(CONCAT('details_',DATE_FORMAT(DATE_SUB(CURDATE(), INTERVAL 1 
    DAY),'%y%m%d')));   
        SET @qsql=CONCAT("SELECT COUNT(*) FROM ",prevDate," INTO @a");
        PREPARE stmt FROM @qsql;
        EXECUTE stmt;
        SELECT @a AS detailcount ;
        DEALLOCATE PREPARE stmt; 


        SET prevDate=(CONCAT('Subdetails_',DATE_FORMAT(DATE_SUB(CURDATE(), 
INTERVAL 1 DAY),'%y%m%d')));   
        SET @qsql=CONCAT("SELECT COUNT(*) FROM ",prevDate," INTO @b");
        PREPARE stmt FROM @qsql;
        EXECUTE stmt;
        SELECT @b AS Subdetails ;
        DEALLOCATE PREPARE stmt; 
        SET totalcount=detailcount+Subdetails;    
        SELECT totalcount;

结果: 获取 totalcount = NULL detailcount = NULL 子细节=某些值

这里有什么错误?

1 个答案:

答案 0 :(得分:0)

执行两个准备好的查询后释放语句给出所需的结果。

 SET prevDate=(CONCAT('details_',DATE_FORMAT(DATE_SUB(CURDATE(), INTERVAL 1 
    DAY),'%y%m%d')));   
        SET @qsql=CONCAT("SELECT COUNT(*) FROM ",prevDate," INTO @a");
        PREPARE stmt FROM @qsql;
        EXECUTE stmt;
        SELECT @a AS detailcount ;       


        SET prevDate=(CONCAT('Subdetails_',DATE_FORMAT(DATE_SUB(CURDATE(), 
INTERVAL 1 DAY),'%y%m%d')));   
        SET @qsql=CONCAT("SELECT COUNT(*) FROM ",prevDate," INTO @b");
        PREPARE stmt FROM @qsql;
        EXECUTE stmt;
        SELECT @b AS Subdetails ;

        DEALLOCATE PREPARE stmt; 

        SET totalcount=detailcount+Subdetails;    
        SELECT totalcount;