下面的代码应该在金额为负数时抛出异常:
create procedure depositMoney(
IN acc integer,
IN amoun integer
)
BEGIN
DECLARE negative CONDITION FOR SQLSTATE '45000';
IF amoun < 0 THEN SIGNAL negative;
END IF;
INSERT INTO account(balance) VALUES amoun where account.number = acc;
END;
但是当我执行时出现 #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 '' at line 6
你能帮我吗?
最佳答案
检查 INSERT 的语法。
尝试:
DELIMITER //
DROP PROCEDURE IF EXISTS `depositMoney`//
CREATE PROCEDURE `depositMoney` (
IN `acc` INTEGER,
IN `amoun` INTEGER
)
BEGIN
DECLARE `negative` CONDITION FOR SQLSTATE '45000';
IF `amoun` < 0 THEN
SIGNAL `negative`
SET MESSAGE_TEXT = 'An error occurred';
END IF;
/*
Check the syntax of the INSERT.
INSERT INTO account(balance) VALUES amoun where account.number = acc;
*/
END//
DELIMITER ;
MySQL 5.5 或更高版本。
测试:
mysql> CALL `depositMoney`(NULL, -1);
ERROR 1644 (45000): An error occurred
关于mysql : Throw an exception inside procedure,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33736828/