MySQL:存储过程语法错误

时间:2017-04-20 09:43:08

标签: mysql

我正在phpmyadmin中编写存储过程,但我在Update语句附近收到语法错误。但是,如果我将更新语句放在if语句中,而不是所有错误都已解决。

问:为什么外部更新语句会导致错误?

CREATE DEFINER=`root`@`localhost` PROCEDURE `tbl_people_update`(IN `p_Id` INT, IN `p_Name` VARCHAR(150), IN `p_Mobile_Number` VARCHAR(100))
    IF CHAR_LENGTH(p_Mobile_Number) <= 0 THEN
     SET p_Mobile_Number = '12345';
    END IF;

    UPDATE tbl_people
     SET 
     Name=p_Name,
     Mobile_Number = p_Mobile_Number
     WHERE
     Id=p_Id;

错误如下:

  

#1064 - 您的SQL语法出错;查看与MariaDB服务器版本对应的手册,以获取正确的语法   'UPDATE tbl_people SET Name = p_Name,Mobile_Number =附近   p_Mobile_Number'第5行'

2 个答案:

答案 0 :(得分:1)

你忘了重新定义分隔符,试试这个:

delimiter ||
CREATE DEFINER=`root`@`localhost` PROCEDURE `tbl_people_update`(IN `p_Id` INT, IN `p_Name` VARCHAR(150), IN `p_Mobile_Number` VARCHAR(100))
    IF CHAR_LENGTH(p_Mobile_Number) <= 0 THEN
     SET p_Mobile_Number = '12345';
    END IF;

    UPDATE tbl_people
     SET 
     Name=p_Name,
     Mobile_Number = p_Mobile_Number
     WHERE
     Id=p_Id;
||
delimiter ;

答案 1 :(得分:1)

DELIMITER $$
CREATE DEFINER=`root`@`localhost` PROCEDURE `tbl_people_update`(IN `p_Id` INT, IN `p_Name` VARCHAR(150), IN `p_Mobile_Number` VARCHAR(100))
BEGIN
     IF CHAR_LENGTH(p_Mobile_Number) <= 0 THEN
     SET p_Mobile_Number = '12345';
    END IF;
    UPDATE tbl_people
     SET 
     Name=p_Name,
     Mobile_Number = p_Mobile_Number
     WHERE
     Id=p_Id;
END$$
DELIMITER ;

尝试以上代码。

希望这会对你有所帮助。