Mysql函数1064错误

时间:2014-05-10 12:08:55

标签: mysql function mysql-error-1064

以下版本适用于旧版本的MySQL。

但我不知道问题出在哪里。

PARAMS:

aId - int usigned
sId - int usigned

功能:

BEGIN

DECLARE test decimal;

SELECT lft into @myLeft FROM `vw_accounts` WHERE accountId = aId;
SELECT rgt into @myRight FROM `vw_accounts` WHERE accountId = aId;
SELECT (rgt - lft + 1) into @myWidth FROM `vw_accounts` WHERE accountId = aId;

SELECT sum(value) as v into test FROM `vw_accounts` 
WHERE lft 
BETWEEN @myLeft AND @myRight AND subjectId = sId;

RETURN test;

END

错误:

The following query has failed: "CREATE FUNCTION `get_all_money_account`(`aId` INT UNSIGNED, `sId` INT UNSIGNED) RETURNS DECIMAL(decimal(12,2)) COMMENT 'vrátí součet všech účtů pod vybraným' NOT DETERMINISTIC READS SQL DATA SQL SECURITY DEFINER BEGIN

DECLARE test DECIMAL;

SELECT lft into @myLeft FROM `vw_accounts` WHERE accountId = aId;
SELECT rgt into @myRight FROM `vw_accounts` WHERE accountId = aId;
SELECT (rgt - lft + 1) into @myWidth FROM `vw_accounts` WHERE accountId = aId;

SELECT sum(value) as v into test FROM `vw_accounts` 
WHERE lft 
BETWEEN @myLeft AND @myRight AND subjectId = sId;

RETURN test;

END"

旧脚本:

CREATE DEFINER=`root`@`localhost` FUNCTION `get_all_money_account`(`aId` INT(10) UNSIGNED, `sId` INT(10) UNSIGNED) RETURNS decimal(12,2)
    READS SQL DATA
    COMMENT 'vrátí součet všech účtů pod vybraným'
BEGIN

DECLARE test float;

SELECT lft into @myLeft FROM `vw_accounts` WHERE accountId = aId;
SELECT rgt into @myRight FROM `vw_accounts` WHERE accountId = aId;
SELECT (rgt - lft + 1) into @myWidth FROM `vw_accounts` WHERE accountId = aId;

SELECT sum(value) as v into test FROM `vw_accounts` 
WHERE lft 
BETWEEN @myLeft AND @myRight AND subjectId = sId;

RETURN test;
END

1 个答案:

答案 0 :(得分:0)

您是否考虑过进行自我加入?

SELECT SUM(b.value)
FROM   vw_accounts a
  JOIN vw_accounts b ON b.lft BETWEEN a.lft AND a.rgt
WHERE  a.accountId = ?
   AND b.subjectId = ?