MySQL游标循环中的语法错误

时间:2014-09-01 20:20:19

标签: mysql database cursor syntax-error

我在使用游标循环查询结果时出现问题。我的目标是使用结果生成JSON数组。我在Fedora上使用MySQL 5.6.20。

我的代码:

DROP FUNCTION IF EXISTS get_quotes_for_customer;
CREATE DEFINER=`root`@`localhost` FUNCTION `get_quotes_for_customer`(c CHAR(6)) RETURNS TEXT
BEGIN

    DECLARE done INT DEFAULT 0;

    DECLARE qcur CURSOR FOR SELECT id, submit_date FROM quote_forms WHERE owner=c;
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
    OPEN qcur;

    SET @output = '[';
    t: LOOP
      FETCH qcur INTO @i, @t;
      IF done=1 THEN
        LEAVE t;
      END IF;
      SET @newrow = concat('{"id": "', @i, '", "timestamp": "', @t, '"}');
      SET @output = concat_ws(@output, @newrow);
    END LOOP;
    SET @output = concat(@output, ']');

    RETURN @output;
  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 '@i, @t;
  IF done=1 THEN
    LEAVE t;
  END IF;
  SET @newrow = ' at line 12

这可能是什么问题?我现在已经把自己的大脑震撼了好几个小时了,错误信息并没有多大帮助。感谢。

0 个答案:

没有答案