无法创建返回某个值的存储例程

时间:2016-01-20 10:37:19

标签: mysql sql database stored-procedures mariadb

我在Maria DB工作。我有存储过程。现在我需要添加一些返回功能。为此,我创建了一个看起来像这样的新函数 -

CREATE FUNCTION `insertChild`(`nodeId` INT, `newNodeName` INT)
    RETURNS INT
    LANGUAGE SQL
    NOT DETERMINISTIC
    CONTAINS SQL
    SQL SECURITY DEFINER
    COMMENT ''
BEGIN

    Rollback;

START TRANSACTION;

    SELECT @myRight := rgt FROM item_tree WHERE item_id = nodeId;

    UPDATE item_tree SET rgt = rgt + 2 WHERE rgt >= @myRight;
    UPDATE item_tree SET lft = lft + 2 WHERE lft > @myRight;
    INSERT INTO item_tree(item_name, lft, rgt) VALUES(newNodeName, @myRight , @myRight + 1);

    RETURN (SELECT (COUNT(parent.item_name) - 1) AS depth FROM item_tree AS node,
        item_tree AS parent
    WHERE node.item_id = last_insert_id() AND 
    node.lft BETWEEN parent.lft AND parent.rgt 
    GROUP BY node.item_name
    ORDER BY node.lft);

COMMIT;

END

但是,这不起作用。它返回一个SQL错误(1415):不允许从函数返回结果集。

这里有什么问题?

提前致谢!

1 个答案:

答案 0 :(得分:0)

我改变了它的回归方式。在变量中设置计数并返回该变量。请检查下面的更新代码

CREATE FUNCTION `insertChild`(`nodeId` INT, `newNodeName` INT)
    RETURNS INT
    LANGUAGE SQL
    NOT DETERMINISTIC
    CONTAINS SQL
    SQL SECURITY DEFINER
    COMMENT ''
BEGIN

    Rollback;

START TRANSACTION;

    SELECT @myRight := rgt FROM item_tree WHERE item_id = nodeId;

    UPDATE item_tree SET rgt = rgt + 2 WHERE rgt >= @myRight;
    UPDATE item_tree SET lft = lft + 2 WHERE lft > @myRight;
    INSERT INTO item_tree(item_name, lft, rgt) VALUES(newNodeName, @myRight , @myRight + 1);

    SELECT @myCount := (COUNT(parent.item_name) - 1) AS depth FROM item_tree AS node,
        item_tree AS parent
    WHERE node.item_id = last_insert_id() AND 
    node.lft BETWEEN parent.lft AND parent.rgt 
    GROUP BY node.item_name
    ORDER BY node.lft;

    RETURN @myCount;

COMMIT;

END