MYSql存储过程Debug Query

时间:2014-04-17 06:14:44

标签: mysql stored-procedures

当我尝试在phpmyadmin上运行mysql存储过程代码时,会生成错误。

错误是:concat附近的语法错误(strWhere,' AND(qxt.topic_id IN(',

) 谢谢!

DELIMITER $$
DROP PROCEDURE IF EXISTS PercentageCalc$$

CREATE PROCEDURE PercentageCalc(topicArray VARCHAR(255),topicIndId int(11),userId VARCHAR(255),userArraySize int(11))

BEGIN
DECLARE x  INT;
SET x = 0;

SELECT topicArray;
SELECT topicIndId;

SET @sql = CONCAT('SELECT COUNT(*) AS total FROM answer as ans
                  INNER JOIN question AS qst ON ans.question_id = qst.question_id
                  INNER JOIN question_topic AS qxt ON qxt.question_id = qst.question_id
                  WHERE (ans.answer_correct_flag = \'yes\')
                  AND (qxt.topic_id IN (\',topicArray,\'))',strwhere);

IF (topicIndId  NOT NULL) THEN

      SET strWhere = concat(strWhere, ' AND (qxt.topic_id IN (',topicIndId,'))');

END IF;

IF (userId  NOT NULL) THEN

            REPEAT

                SET strWhere = concat(strWhere, ' AND (ans.user_id IN (',userId[X],'))');
        SET  x = x + 1;
                    UNTIL x  > userArraySize

            END REPEAT

END IF;

PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

   END$$
DELIMITER ;

CALL PercentageCalc('1,2,3','1,2','1','1');

0 个答案:

没有答案