1

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

   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 

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

4

1 回答 1

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 ;
于 2012-09-30T05:43:01.780 回答