MySQL存储过程案例语句语法错误-续

时间:2018-09-08 09:37:15

标签: mysql if-statement stored-procedures syntax-error

该过程包含语法错误。请帮助我找到错误。 MySQL Workbench表示这有8个错误,因此无法应用于数据库。我已经发布了这个问题。人们告诉我将CASE语句更改为IF,即使更改后错误仍然存​​在。我不知道如何将此帖子嵌套在原始问题下。实际的帖子已发布在此link中。 经修订的语法按要求发布 @Caius Jard

CREATE PROCEDURE `calculate_amount` (in IN_book_id INT,  in IN_qty INT )
BEGIN
-- declare
DECLARE m_prdct VARCHAR(10);
DECLARE m_cust_id INT(5);

-- select into
SELECT 
    Product
FROM
    Bookings
WHERE
    Book_id = IN_book_id INTO m_prdct;

SELECT 
    Cust_id
FROM
    Bookings
WHERE
    Book_id = IN_book_id INTO m_cust_id;

-- conditionals & action
IF (m_prdct = '20ltr') THEN
INSERT INTO Amount(Cust_id,Book_id,Can,300ml,500ml,1lit,2lit) VALUES(m_cust_id,IN_book_id,(SELECT Rate.Can*Bookings.Qty FROM Rate,Bookings WHERE Bookings.Book_id=IN_book_id),0,0,0,0); 

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

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

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

ELSE 
INSERT INTO Amount(Cust_id,Book_id,Can,300ml,500ml,1lit,2lit) VALUES(m_cust_id,IN_book_id,0,0,0,0,(SELECT Rate.2lit*Bookings.Qty FROM Rate,Bookings WHERE Bookings.Book_id=IN_book_id)); 
-- end
END IF;
END

1 个答案:

答案 0 :(得分:0)

您需要引用以数字开头的标识符:

INSERT INTO Amount(Cust_id,Book_id,Can,`300ml`,`500ml`,`1lit`,`2lit`) ...