在存储过程中使用IF条件的UPDATE的MySQL语法?

时间:2022-03-09 22:51:35

I am trying run a stored procedure but its getting failed due to IF condition.I have tried with different ways still no success. can anyone let me know, how to use IF condition in the stored procedure.

我正在尝试运行存储过程但由于IF条件而失败。我尝试过不同的方法仍然没有成功。任何人都可以让我知道,如何在存储过程中使用IF条件。

DELIMITER $$

USE `testdb`$$

DROP PROCEDURE IF EXISTS `change_parent`$$

CREATE DEFINER=`root`@`localhost` PROCEDURE `change_parent`(
    IN new_parent BIGINT(20),
    IN folder__id BIGINT(20)
)
BEGIN
    SELECT 
        directory_path,nav_depth,id
    INTO 
        @dirpath,@depth,@parent
    FROM folders 
    WHERE id = new_parent;
    UPDATE folders


        IF (folder__id != @parent) THEN 
        SET     directory_path = CONCAT(@dirpath,'/',title),
            nav_depth = @depth+1,
            parent__id = @parent
        END IF;
    WHERE id = folder__id;

END$$

DELIMITER ;

Here is the error message

这是错误消息

Error Code: 1064 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: 'IF folder__id != @parent THEN SET directory_path = CONCAT(@dirpath,'/',title' at line 13

错误代码:1064您的SQL语法有错误;检查与您的MySQL服务器版本对应的手册,以便在附近使用正确的语法:'IF folder__id!= @parent THEN SET directory_path = CONCAT(@dirpath,'/',title'在第13行

1 个解决方案

#1


IF (folder__id != @parent) THEN
    UPDATE folders
            SET ...
        WHERE id = folder__id;
END IF;

#1


IF (folder__id != @parent) THEN
    UPDATE folders
            SET ...
        WHERE id = folder__id;
END IF;