DELIMITER ;;
CREATE PROCEDURE `CANCEL_ORDER`(IN order_id INT, IN buyer_user_id INT)
BEGIN
SET autocommit=0;
START TRANSACTION;
SELECT customer_user_id INTO @userid FROM orders WHERE id=order_id;
IF @userid=buyer_user_id THEN
UPDATE orders SET status='failed',canceled_at=UNIX_TIMESTAMP(CURRENT_TIMESTAMP()) WHERE id=order_id;
COMMIT;
ELSE
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'Error: Customer ID in orders does not match with given buyer_user_id', MYSQL_ERRNO = 1003;
ROLLBACK;
END IF;
END;;
DELIMITER ;
第2行的错误1064(42000):您的SQL语法出错; 检查与您的MySQL服务器版本对应的手册 在'SET autocommit = 0附近使用正确的语法; 开始交易; 在第3行选择customer_user_id INTO'
嗨frds, 如何纠正它
答案 0 :(得分:2)
“DELIMITER ;;”无效
使用:DELIMITER New_delimiter Old_Delimiter_To_Finish_Line
两个分隔符必须是不同的
在程序的代码中使用旧分隔符,但用新代码完成; 恢复旧分隔符
DELIMITER $;
CREATE PROCEDURE `CANCEL_ORDER`(IN order_id INT, IN buyer_user_id INT)
BEGIN
SET autocommit=0;
START TRANSACTION;
SELECT customer_user_id INTO @userid FROM orders WHERE id=order_id;
IF @userid=buyer_user_id THEN
UPDATE orders SET status='failed',canceled_at=UNIX_TIMESTAMP(CURRENT_TIMESTAMP()) WHERE id=order_id;
COMMIT;
ELSE
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'Error: Customer ID in orders does not match with given buyer_user_id', MYSQL_ERRNO = 1003;
ROLLBACK;
END IF;
END;$
DELIMITER ; $
操作如下:tokenisateur将使用当前分隔符;
提取第一个查询
分隔符变为$
tokenisateur将使用当前分隔符$
提取第二个查询并忽略旧分隔符;
因此,不会分离存储过程的不同行。
然后,tokenisateur将使用Still active delimiter $
分隔符提取第三个请求并成为;
这意味着要完成最后一个命令必须是一个;
其他事项:
START TRANSACTION;
会自动更改autocommit
@userid
是
未定义
canceled_at = UNIX_TIMESTAMP (CURRENT_TIMESTAMP ())
:如果该字段是TIMESTAMP,则完全没用。 TIMESTAMP字段会自动更新。
答案 1 :(得分:1)
您可以使用以下方法:
DELIMITER $$
USE `test`$$
DROP PROCEDURE IF EXISTS `cancel_order`$$
CREATE DEFINER=`root`@`%` PROCEDURE `cancel_order`(IN order_id INT, IN buyer_user_id INT)
BEGIN
DECLARE v_cancel BOOLEAN DEFAULT FALSE;
SET autocommit=0;
START TRANSACTION;
SELECT TRUE INTO v_cancel FROM orders WHERE id=order_id AND customer_user_id=buyer_user_id;
IF v_cancel THEN
UPDATE orders SET STATUS='failed',canceled_at=NOW() WHERE id=order_id;
COMMIT;
ELSE
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'Error: Customer ID in orders does not match with given buyer_user_id', MYSQL_ERRNO = 1003;
ROLLBACK;
END IF;
END$$
DELIMITER ;