0
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 行的 ERROR 1064 (42000):您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以获取在 ' SET autocommit=0; 附近使用的正确语法;开始交易;SELECT customer_user_id INTO ' 在第 3 行

嗨 frds,如何纠正它

4

2 回答 2

2

“分隔符;;” 无效

利用: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 delimiter 提取第三个请求$并变为;这意味着要完成最后一个命令必须是一个;

其他事情:

  • START TRANSACTION;自动更改autocommit

  • @userid没有定义

  • canceled_at = UNIX_TIMESTAMP (CURRENT_TIMESTAMP ()):如果该字段是 TIMESTAMP,则完全没用。TIMESTAMP 字段会自动更新。

于 2013-06-16T13:51:03.300 回答
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 ;
于 2013-06-21T12:39:06.443 回答