1

I am writing one procedure in order to get data from database .

Here is my 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

When I am executing the above code I am getting an error as below .

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

I know this is the syntax problem but not able to figure out what I am doing wrong here .

Please help me out .

ALso do let me know if there any way to print the query which is executing in procedure for debugging purpose .

4
  • Joins should come before the WHERE clause Commented Jun 30, 2014 at 6:54
  • remove the erroneous line and Uncomment the same above line. Commented Jun 30, 2014 at 6:56
  • @Ravinder I removed the commented code and tried but still getting the same error Commented Jun 30, 2014 at 6:59
  • What is the output of select @sqlCalls;? place this statement before prepare and run. Commented Jun 30, 2014 at 7:22

1 Answer 1

1

Looks like you have a wrong order in the generated statement:

You get a select ... where ... left join .... This is wrong syntax.

Sign up to request clarification or add additional context in comments.

4 Comments

Mean I have to do left join first then followed by where
@user1534577 Yes thats what i mean.
Please see the commented line after select statement in above procedure I tried that way getting the same error
@user1534577 you have a GROUP BY at the end of join. That must be after the WHERE statement.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.