MySQL Stored Procedure Case Statement Syntax Error - Contd 2 - mysql

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.

Related

MySQL creating procedure getting error I cannot figure out why

I want to create a procedure for a MySql database but I get this error below and after 2h of research I still cannot figure out why.
The code is the following :
delimiter //
create procedure mitarbeiter_projekt (proj_name varchar(20), mitarb_name varchar(20))
begin
declare pruef_id int;
declare new_id int;
declare mitarb_id int;
select count(id) from t_proj where name = proj_name into pruef_id;
select id from t_ma_dt where name = mitarb_name into mitarb_id;
if pruef_id = 0 then
select max(id) + 1 from t_proj into new_id;
insert into t_proj (id, name) values (new_id, proj_name);
insert into t_ma_proj (ma_id, proj_id )values (mitarb_id, new_id);
else
select id from t_proj where name = proj_name into new_id;
insert into t_ma_proj (ma_id, proj_id) values (mitarb_id, new_id);
end //
"ERROR 1064 (42000): You have an error in your SQL syntax; check the
manual that corresponds to your MariaDB server version for the right
syntax to use near '' at line 15"
all of the above tables I used exist in my database.
Thankful for any help!

MySQL Stored Procedure Case Statement Syntax Error - Contd

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`) ...

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;

SQL If Statment / select into in a stored procedure

I cannot understand why the following SQL procedure will not store on my database and is reporting an error, I've been at it for ages and am totally puzzled.
DELIMITER $$
CREATE PROCEDURE add_brand(IN inBrandName TEXT)
BEGIN
DECLARE brandexists INT DEFAULT 0;
SELECT count(*) WHERE BrandName = inBrandName INTO brandexists;
IF brandexists = 1 THEN
select "exists";
ELSE
INSERT INTO tblBrand (BrandName) VALUES (inBrandName);
SELECT LAST_INSERT_ID();
END IF;
END
The error i'm getting is this:
#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 'WHERE BrandName = inBrandName INTO brandexists; IF brandexists = 1 THEN sele' at line 6
any ideas?
You are missing a table where you select from:
SELECT count(*) WHERE BrandName = inBrandName INTO brandexists;
^---here (from some_table)

Mysql sql error #1064

check the manual that corresponds to your MySQL server version for the right syntax to use near: 'SELECT count(*) FROM capacity
WHERE
server=NEW.server
AND
feed' at line 2
my code:
DROP TRIGGER IF EXISTS newsmonitoringrawdata.TNTInsertionTrigger;
CREATE TRIGGER TgtInsertionTrigger BEFORE INSERT ON tnews
FOR EACH ROW BEGIN
SET #isRecordCreated =
SELECT count(*) FROM capacity
WHERE
server=NEW.server
AND
feed=NEW.Feed
AND
date_Format(streamdt,'%Y-%m-%d %H:%i')=date_Format(NEW.StreamDT,'%Y-%m-%d %H:%i');
IF #isRecordCreated>0 THEN
UPDATE capacity
SET MsgNumber=MsgNumber+1,
size=size+NEW.MSize
WHERE
server=NEW.server
AND
feed=NEW.feed
and
date_Format(streamdt,'%Y-%m-%d %H:%i')=date_Format(NEW.StreamDT,'%Y-%m-%d %H:%i');
ELSE
INSERT INTO capacity(server, feed, streamdt, msgNumber, size)
VALUES
(
NEW.server,
NEW.feed,
date_Format(NEW.StreamDT,'%Y-%m-%d %H:%i:00'),
1,
NEW.size
);
END IF;
END
SET #isRecordCreated = (
SELECT count(*) FROM capacity
WHERE
server=NEW.server
AND
feed=NEW.Feed
AND
date_Format(streamdt,'%Y-%m-%d %H:%i')=date_Format(NEW.StreamDT,'%Y-%m-%d %H:%i')
);
You have to add parantheses () around the query.