错误代码1329,SQL状态02000:无数据 - 用于函数

时间:2014-12-25 05:21:53

标签: mysql sql

我正在使用mysql 5.0.27。我写了以下函数。

DELIMITER |

CREATE FUNCTION getDebit (trip1 INT UNSIGNED)

RETURNS DOUBLE

BEGIN
DECLARE oldName DOUBLE DEFAULT 0;
DECLARE done INT DEFAULT 0;
SELECT SUM(COALESCE(amt,0)) INTO oldName 
       FROM  expenses where head IN(SELECT distinct name 
       FROM head where payType='Debit') AND trip=trip1 GROUP BY trip;

DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;

RETURN oldName;

END

DELIMITER ;

它提供错误Error code 1064, SQL state 42000: 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 'DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;END' at line 11

请帮帮我。

1 个答案:

答案 0 :(得分:0)

您在那里的错误:

SELECT SUM(COALESCE(amt,0)) INTO oldName 
   FROM  expenses where head IN(SELECT distinct name 
   FROM head where payType='Debit') AND trip=trip1 GROUP BY trip;

必须在所有声明之后使用此调用。 当mysql在“ SELECT”之后看到“ DECLARE”时发生错误 另外,您必须声明变量“完成”

也许你是说:

/* Add this */
DECLARE done int;

/* Your code*/
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;

/* Add this */
SET done  = 0;

/* your code next */
SUM(COALESCE(amt,0)) INTO oldName 
       FROM  expenses where head IN(SELECT distinct name 
       FROM head where payType='Debit') AND trip=trip1 GROUP BY trip;

return oldName;

但是!!! 您对继续处理程序的声明是没有用的。

SUM()将始终是返回值。 如果未找到任何内容,则为0。

“完成”永远不会改变