MySQL Stored Procedure Case Statement Syntax Error

2019-09-02 06:22发布

问题:

I'm trying to create a stored procedure that calculates the amount by multiplying the ordered quantity & the rate of the product from different tables called Rate and getting Quantity from the Bookings Table.After calculating I want it to be inserted into another Table 'Amount'. I am having totally 5 products. If for example product = 'abc', then put amount for 'abc' and insert zero for the rest of the products' amount. Herein, I am using a Case Statement which throws syntax error. Kindly please offer me your help.

DELIMITER $$
CREATE PROCEDURE `calculate_amount` (IN IN_book_id INT,IN IN_qty INT )
-- begin
BEGIN
-- declare
DECLARE prdct VARCHAR(10);
DECLARE cust_id INT(5);
-- select into
SELECT Product FROM Bookings WHERE Book_id=IN_book_id INTO prdct;
SELECT Cust_id FROM Bookings WHERE Book_id=IN_book_id INTO cust_id;
-- conditionals & action
CASE prdct
WHEN "20ltr"
THEN INSERT INTO Amount(Cust_id,Book_id,Can,300ml,500ml,1lit,2lit) VALUES(cust_id,IN_book_id,(SELECT Rate.Can*Bookings.Qty FROM Rate,Bookings WHERE Bookings.Book_id=IN_book_id),0,0,0,0);

WHEN "300ml"
THEN INSERT INTO Amount(Cust_id,Book_id,Can,300ml,500ml,1lit,2lit) VALUES(cust_id,IN_book_id,0,(SELECT Rate.300ml*Bookings.Qty FROM Rate,Bookings WHERE Bookings.Book_id=IN_book_id),0,0,0); 

WHEN "500ml"
THEN 
INSERT INTO Amount(Cust_id,Book_id,Can,300ml,500ml,1lit,2lit) VALUES(cust_id,IN_book_id,0,0,(SELECT Rate.500ml*Bookings.Qty FROM Rate,Bookings WHERE Bookings.Book_id=IN_book_id),0,0); 

WHEN "1ltr"
THEN 
INSERT INTO Amount(Cust_id,Book_id,Can,300ml,500ml,1lit,2lit) VALUES(cust_id,IN_book_id,0,0,0,(SELECT Rate.1lit*Bookings.Qty FROM Rate,Bookings WHERE Bookings.Book_id=IN_book_id),0); 

WHEN "2ltr"
THEN 
INSERT INTO Amount(Cust_id,Book_id,Can,300ml,500ml,1lit,2lit) VALUES(cust_id,IN_book_id,0,0,0,0,(SELECT Rate.2lit*Bookings.Qty FROM Rate,Bookings WHERE Bookings.Book_id=IN_book_id)); 

ELSE 
    BEGIN
    END;
END CASE;
-- end
END;$$

DELIMITER ;

回答1:

In SQL (in general), CASE is not used for program conditional flow, it is used to return singular values in variable assignment. MySQL does allow it but I recommend to avoid using it as other database systems don't support it

Typical scenarios:

--doing varied logic:
name = CASE 
    WHEN num = 1 THEN 'one' 
    WHEN othernum = 2 THEN 'two' 
END

--testing a single variable for being equal to values:
name = CASE num 
  WHEN 1 THEN 'one' 
  WHEN 2 THEN 'two' 
END

Avoid this:

CASE 
  WHEN num = 1 THEN name = 'one' 
  WHEN num = 2 THEN name = 'two' 
END

If you're after keeping your database skills cross portable, essentially the only thing you should put inside the "result" of a case is a value, not a statement

IF ELSEIF ELSE is used for conditional program flow in major databases, the MySQL docs for it are here:

https://dev.mysql.com/doc/refman/8.0/en/if.html

As another aside, prefer use of ' for strings, not " - double quotes are another MySQL deviation from standard