程序中的SQL语法错误

时间:2014-06-30 06:51:51

标签: mysql sql procedure

我正在编写一个程序,以便从数据库中获取数据。

这是我的程序。

CREATE DEFINER=`root`@`localhost` PROCEDURE `getCdrListnew`(from_date DATETIME, to_date DATETIME, 
 p_category VARCHAR(255), p_callType VARCHAR(255), p_businessId VARCHAR(255), p_dnc VARCHAR(255),
 p_cli VARCHAR(255), p_dni VARCHAR(255), p_callNumber VARCHAR(255), p_dialType VARCHAR(255), p_contractType VARCHAR(255) )
BEGIN
       DECLARE a INT ;
       DECLARE setFilter INT DEFAULT FALSE;
    DECLARE CONTINUE HANDLER FOR NOT FOUND 
       SET a = 0;
     BEGIN
END;


    SET @sqlCalls = 'SELECT agentcdr.isdnCauseCode,COUNT(*) as count  FROM cdr';  
 -- SET @sqlCalls= CONCAT(@sqlCalls,' LEFT JOIN agentcdr ON cdr.idCDR=agentcdr.cdr_idCDR ');


        IF( (from_date IS NOT NULL AND from_date != '')&& (to_date IS NOT NULL  AND to_date != '') ) THEN
   SET @sqlCalls= CONCAT(@sqlCalls,' WHERE cdr.createdDt >=\'',from_date,'\' AND cdr.createdDT<=', '\'',to_date,'\'');
   SET setFilter = TRUE;
  END IF; 

  SET @sqlCalls= CONCAT(@sqlCalls,' LEFT JOIN agentcdr ON cdr.idCDR=agentcdr.cdr_idCDR GROUP BY agentcdr.isdnCauseCode ');
  PREPARE prepsqlstr1 FROM @sqlCalls;
  EXECUTE prepsqlstr1;
END

当我执行上面的代码时,我收到如下错误。

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'LEFT JOIN agentcdr ON cdr.idCDR=agentcdr.cdr_idCDR GROUP BY agentcdr.isdnCauseCo' at line 1

我知道这是语法问题,但无法弄清楚我在这里做错了什么。

请帮帮我。

如果有任何方法可以打印出在调试过程中执行的查询,也可以告诉我。

1 个答案:

答案 0 :(得分:1)

在生成的语句中看起来订单错误:

你得到select ... where ... left join ...。这是错误的语法。