MySQL Stored Procedure Case Statement Syntax Error - Contd - mysql

The Procedure contains syntax error. Please help me in finding the error. The mySQL Workbench says that this has 8 errors and this cannot be applied to the database. I have already posted this question. People told me to change the CASE statement to IF.Even after changes, the error persists. I don't know how to nest this post under the original question. The actual post was posted in this link.
Revised syntax is posted as requested #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

You need to quote identifiers that starts with a digit:
INSERT INTO Amount(Cust_id,Book_id,Can,`300ml`,`500ml`,`1lit`,`2lit`) ...

Related

MySQL Stored Procedure Case Statement Syntax Error

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

MySQL Stored Procedure Case Statement Syntax Error - Contd 2

One Syntax Error in the following code.
The response of the mysql was
Error 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 ''300ml','500ml','1lit','2lit')
VALUES(m_cust_id,IN_book_id,(SELECT Rate.Can*Book' at line 24 SQL
Statement
The following is the procedure.
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
The script applied successfully even if it showed error. The Back Tick to column names starting with digits solved the issue. Upon Refreshing, it got changed from _SYNTAX_ERROR to its actual name.

ERROR 1064 (42000): check the manual that corresponds to your MySQL server version for the right syntax to use near 'END'

here is mycode
i am trying to create a trigger after insert on a table say product table to a change traker table called audit table
like this
DELIMITER //
CREATE TRIGGER product_table_after_insert
AFTER INSERT
ON product_table FOR EACH ROW
BEGIN
DECLARE l_product_description varchar(500);
DECLARE l_product_number int;
set #l_table_name = 'product_table';
set #l_action = 'INSERT';
set #l_table_column = 'all columns';
set #l_description = 'new row inserted';
select p.product_description ,p.product_number into #l_product_description, #l_product_number from product_table p where p.product_description = (select max(pg.product_number)from product_table pg);
-- Insert record into audit table
INSERT INTO audit_table_test
( table_name,
changed_row_id,
action,
table_column,
change_desciption,
change_time
)
VALUES
( l_table_name,
l_product_number,
l_action,
l_table_column,
l_description,
SYSDATE()
)
END
//
DELIMITER ;
this is the error i am getting
i tried all these ways
used | ,S etc instead of // and
removed ; after end, placed // or \ or $$ together and below end
nothing works, some one please help me
ERROR 1064 (42000): 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 32
some one please help me
Place sql terminator ";" after create table statement
remove ";" after end delimeter.
So your code should be as per below,
DELIMITER //
CREATE TRIGGER product_table_after_insert
AFTER INSERT
ON product_table FOR EACH ROW
BEGIN
DECLARE l_product_description varchar(500);
DECLARE l_product_number int;
set #l_table_name = 'product_table';
set #l_action = 'INSERT';
set #l_table_column = 'all columns';
set #l_description = 'new row inserted';
select p.product_description ,p.product_number into #l_product_description, #l_product_number from product_table p where p.product_description = (select max(pg.product_number)from product_table pg);
-- Insert record into audit table
INSERT INTO audit_table_test
( table_name,
changed_row_id,
action,
table_column,
change_desciption,
change_time
)
VALUES
( l_table_name,
l_product_number,
l_action,
l_table_column,
l_description,
SYSDATE()
);
END
//
DELIMITER
The solution provided by #juergen in comments is working fine, that is, Add a ; after the insert statement (before the END) Thanks for finding my mistake. I was looking for it for about 4 hours so Answer is here:
DELIMITER //
CREATE TRIGGER product_table_after_insert
AFTER INSERT
ON product_table
FOR EACH ROW
BEGIN
DECLARE l_product_description varchar(500);
DECLARE l_product_number int;
set #l_table_name = 'product_table';
set #l_action = 'INSERT';
set #l_table_column = 'all columns';
set #l_description = 'new row inserted';
select p.product_description ,p.product_number
into #l_product_description, #l_product_number
from product_table p
where p.product_description =
(select max(pg.product_number)
from product_table pg);
-- Insert record into audit table
INSERT INTO audit_table_test
( table_name,
changed_row_id,
action,
table_column,
change_desciption,
change_time
)
VALUES
( l_table_name,
l_product_number,
l_action,
l_table_column,
l_description,
SYSDATE()
); //<<---- Semicolon needed to be here
END
//
DELIMITER;

How do I create a stored procedure in MySQL that will select data and insert new values?

Hi I am trying to create a database that will check out document numbers per project. I am trying to keep the db server action limited to stored procedures.
The numbering system goes
XXXX-YY-ZZZZ
XXXX is the job number
YY is the document type table reference
ZZZZ is the document specific number and increments from 1 up for each type in each job...
Because of the numbering system, I can not use an auto incremented column... That would be nice... I would have to generate a table for each document type, for each job... unless anyone sees anything I do not? I am pretty new at this, so any help would be appreciated.
Here is what I have for my procedure that I am having issues with.
DELIMITER $$
DROP PROCEDURE IF EXISTS ADD_NEW_DOC$$
CREATE DEFINER=RMDNA#localhost PROCEDURE ADD_NEW_DOC(IN in_job INT, IN in_type VARCHAR(2), IN in_name VARCHAR(32), IN in_desc VARCHAR(512))
BEGIN
SELECT MAX(doc_id) INTO #highnum FROM Doc_Entries WHERE job_id = in_job AND doc_type_ID = in_type$$
IF #highnum = null THEN SET #newnum = 1$$
ELSE SET #newnum = #highnum + 1$$
END IF$$
insert into Doc_Entries (job_id, doc_type_ID, doc_id, doc_name, doc_desc) values (in_job, in_type, #newnum, in_name, in_desc)$$
SELECT #newnum$$
END$$
DELIMITER ;
and the error I am getting is:
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 2
UPDATE:
My attempt has refined...
DROP PROCEDURE IF EXISTS ADD_NEW_DOC;
DELIMITER //
CREATE DEFINER=RMDNA#localhost PROCEDURE ADD_NEW_DOC(IN in_job INT, IN in_type VARCHAR(2), IN in_name VARCHAR(32), IN in_desc VARCHAR(512))
BEGIN
SELECT MAX(doc_id) INTO #highnum FROM Doc_Entries WHERE job_id = in_job AND doc_type_ID = in_type;
IF (#highnum is null) THEN SET #newnum = 1;
ELSE SET #newnum = #highnum + 1;
END IF;
insert into Doc_Entries (job_id, doc_type_ID, doc_id, doc_name, doc_desc) values (in_job, in_type, #newnum, in_name, in_desc);
SELECT #newnum;
END//
DELIMITER ;
Working good now. Learned how to use delimiter properly and fixed a few things that then became obvious.

MySQL stored procedure if statement not working

I have the following stored procedure in MySQL
CREATE DEFINER=`test_db`#`%` PROCEDURE `ADD_ATTENDANCE`(IN `programID` INT, IN `clientID` INT, IN `insDate` DATETIME, IN `updDate` DATETIME, IN `insUser` INT, IN `updUser` INT, IN `lessonDate` DATE, IN `lessonTime` TIME)
LANGUAGE SQL
NOT DETERMINISTIC
CONTAINS SQL
SQL SECURITY DEFINER
COMMENT 'Add attedance to my calendar'
BEGIN
DECLARE max_sub, availability INT;
DECLARE cursor_max_sub CURSOR FOR SELECT max_sub FROM app_lesson WHERE id = programID;
DECLARE cursor_availability CURSOR FOR SELECT count(*) FROM attendance WHERE program_id = programID AND lesson_date = lessonDate AND lesson_time = lessonTime;
OPEN cursor_max_sub;
OPEN cursor_availability;
read_loop: LOOP
FETCH cursor_max_sub INTO max_sub;
FETCH cursor_availability INTO availability;
IF (availability < max_sub) THEN
insert into attendance (program_id, client_id, ins_date, upd_date, ins_user, upd_user, lesson_date, lesson_time)
values (programID, clientID, insDate, updDate, insUser, updUser, lessonDate, lessonTime);
LEAVE read_loop;
ELSE
insert into attendance_hold (program_id, client_id, ins_date, upd_date, ins_user, upd_user, lesson_date, lesson_time)
values (programID, clientID, insDate, updDate, insUser, updUser, lessonDate, lessonTime);
END IF;
END LOOP;
CLOSE cursor_max_sub;
CLOSE cursor_availability;
END;
Even though the cursor_max_sub is equal to 6 and the cursor_availability is equal to 4 my procedure always executes the else insert statement. Can you please help me out?
Thanks!!!
OK that was tricky... For some reason when i change the max_sub variable into maxNumberOfSubscription everything worked perfectly... Is max_sub some kind of reserved key word for MySQL or there was a complication because my variable had the same name with the returned field of select statement?