简体   繁体   中英

Mysql function 1064 error

The below works for me with older versions of MySQL.

But I have no idea where the problem can be.

Params:

aId - int usigned
sId - int usigned

Function:

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

Error:

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"

Old script:

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

Have you considered making a self-join instead?

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 = ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM