useyourillusiontoo
useyourillusiontoo

Reputation: 1367

SQL IF ELSE Statement not executing

I've the following prepared statement that I'm trying to create for my shopping cart but it won't execute correctly. It reports the following error and I don't know how to fix it:

#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 '; END $$' at line 25

As far as I know the IF statement syntax is correct but it just wont work.

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 $$

Upvotes: 0

Views: 528

Answers (2)

Shijin TR
Shijin TR

Reputation: 7768

Remove $$ after END $$ and add space between END and IF

    END IF;
    END $$

Upvotes: 1

Ryan Guill
Ryan Guill

Reputation: 13896

It should be END IF not ENDIF on the second line from the bottom.

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 $$

Upvotes: 0

Related Questions