Mysql:存储过程中出现#1064错误

时间:2012-09-30 05:41:48

标签: mysql stored-procedures syntax

每当我尝试创建以下存储过程时。

   CREATE PROCEDURE purchaseItem (IN productID INT, IN quantity INT, IN price DECIMAL(10,2) , IN memID INT)
    BEGIN
        DECLARE qnty INT DEFAULT 0;
            SELECT p_Unit INTO qnty FROM product WHERE p_ID = productID;
            IF qnty >= quantity && qnty != 0 THEN
                INSERT INTO purchase VALUES (NULL, productID, quantity, price, memID, 0, NULL);
                    UPDATE product SET p_Unit = (qnty - quantity) WHERE p_ID = productID;
            END IF;
    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 3 

有人可以指出我在哪里做错了。 谢谢!

1 个答案:

答案 0 :(得分:1)

不要忘记更改DELIMITER,请在不使用SELECT..INTO的情况下尝试以下查询

   DELIMITER $$
   CREATE PROCEDURE purchaseItem 
    (
      IN productID INT, 
      IN quantity INT, 
      IN price DECIMAL(10,2) , 
      IN memID INT
    )
    BEGIN
            DECLARE qnty INT;
            SET qnty = (SELECT p_Unit FROM product WHERE p_ID = productID);
            IF qnty >= quantity && qnty != 0 THEN
                INSERT INTO purchase VALUES 
                      (NULL, productID, quantity, price, memID, 0, NULL);
                UPDATE product 
                SET p_Unit = (qnty - quantity) 
                WHERE p_ID = productID;
            END IF;
    END$$
    DELIMITER ;