SQL IF ELSE语句未执行

时间:2013-05-28 12:11:02

标签: mysql sql stored-procedures

我有以下准备好的声明,我正在尝试为我的购物车创建,但它无法正确执行。它报告以下错误,我不知道如何解决它:

#1064 - 您的SQL语法出错;检查与您的MySQL服务器版本对应的手册,以便在'附近使用正确的语法; END $$'在第25行

据我所知,IF语句的语法是正确的,但它不会起作用。

DELIMITER $$
CREATE PROCEDURE shopping_cart_add_product(IN inCartId CHAR(32),
IN inProductId INT, IN inAttributes VARCHAR(1000))
BEGIN
DECLARE productQuantity INT;

SELECT quantity
FROM shopping_cart
WHERE cart_id = inCartId
AND product_id = inProductId
AND attributes = inAttributes
INTO productQuantity;

IF productQuantity IS NULL THEN
INSERT INTO shopping_cart(cart_id, product_id, attributes,
productQuantityuantity, added_on)
VALUES (inCartId, inProductId, inAttributes, 1, NOW());

ELSE  
UPDATE shopping_cart
SET quantity = quantity + 1, buy_now = true
WHERE cart_id = inCartId
AND product_id = inProductId
AND attributes = inAttributes;
ENDIF;
END $$

2 个答案:

答案 0 :(得分:1)

在END $$之后删除$$并在END和IF之间添加空格

    END IF;
    END $$

答案 1 :(得分:0)

从底部开始的第二行应该是END IF而不是ENDIF。

http://dev.mysql.com/doc/refman/5.5/en/if.html

...
ELSE  
UPDATE shopping_cart
SET quantity = quantity + 1, buy_now = true
WHERE cart_id = inCartId
AND product_id = inProductId
AND attributes = inAttributes;
END IF;
END $$