MySQL Error 1064 when create function - mysql

When I am trying to create a function in mysql 5.7.20, I met error 1064:
delimiter //
create function add_favorstocks (
uid_int INT,
stockid_char CHAR(20),
added_date CHAR(20)
)
returns INT
begin
declare ret INT;
case
when exists (select 1 from user_favorstocks where uid=uid_int and stockid = stockid_char)
then begin
insert into user_favorstocks (uid, stockid, added_date) values (uid_int, stockid_char, added_date);
set ret=1;
end
else
begin
case (select is_deleted from user_favorstocks where uid=uid_int and stockid = stockid_char)
when 0 then set ret=0;
else begin
update user_favorstocks set is_deleted=0, db_update_time=now() where uid=uid_int and stockid=stockid_char;
set ret=3;
end;
end
end
end
return ret
end//
delimiter ;
The error message is
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 'else
begin
case (select is_deleted from user_favorstocks whe' at line 15
Does this because that I am using begin...end in an else clause?
I found the example in MySQL's documentation:
DELIMITER |
CREATE PROCEDURE p()
BEGIN
DECLARE v INT DEFAULT 1;
CASE v
WHEN 2 THEN SELECT v;
WHEN 3 THEN SELECT 0;
ELSE
BEGIN
END;
END CASE;
END;
|
It seems valid to use a begin...end statement in an else clause.

Try:
DROP FUNCTION IF EXISTS `add_favorstocks`;
delimiter //
create function add_favorstocks (
uid_int INT,
stockid_char CHAR(20),
added_date CHAR(20)
)
returns INT
begin
declare ret INT;
case when exists (select 1
from user_favorstocks
where uid=uid_int and
stockid = stockid_char)
then begin
insert into user_favorstocks
(uid, stockid, added_date)
values
(uid_int, stockid_char, added_date);
set ret=1;
-- end <-- missing a semicolon
end;
else
begin
case (select is_deleted
from user_favorstocks
where uid=uid_int and
stockid = stockid_char)
when 0 then set ret=0;
else begin
update user_favorstocks
set is_deleted=0,
db_update_time=now()
where uid=uid_int and
stockid=stockid_char;
set ret=3;
end;
-- end <-- missing CASE and a semicolon
end case;
-- end <-- missing a semicolon
end;
-- end <-- missing CASE and a semicolon
end case;
-- return ret <-- missing a semicolon
return ret;
end//
delimiter ;

Related

MySQL Function getting Syntax Error

I am trying to create the below function in MySQL but getting syntax error.
I am not able to find the solution, would be grateful for some help
CREATE FUNCTION `test`.`pro`(depart_id int) RETURNS varchar
BEGIN
DECLARE title varchar;
if depart_id = 1 then
set title='IT Department';
else if depart_id = 2 then
set title='HR Department';
else
set title='Admin';
end if;
return title;
END$$
DELIMITER ;
You have several syntax errors in your script:
varchar must have a length
You should define DELIMITER $$ first
It's not else if, but elseif
Try this;)
DELIMITER $$
CREATE FUNCTION `test`.`pro`(depart_id int) RETURNS varchar(10)
BEGIN
DECLARE title varchar(10);
if depart_id = 1 then
set title='IT Department';
elseif depart_id = 2 then
set title='HR Department';
else
set title='Admin';
end if;
return title;
END $$
DELIMITER ;

mysql procedure error in IF..THEN statement

So I have this procedure to calculate freights. I need to select two values from matching row. And according to condition based on in_quantity, out of these two previously selected values one will be set to out_total.
DELIMITER $$
CREATE PROCEDURE freight_calc(
IN in_delivery_location VARCHAR(100),
IN in_category_id INT(11),
IN in_quantity INT(11),
OUT out_total DECIMAL(10,2)
)
BEGIN
DECLARE val1 DECIMAL(10,2);
DECLARE val2 DECIMAL(10,2);
SELECT col1,col2 INTO val1, val2
FROM `freight_rules` fr
WHERE fr.category_id = in_category_id AND fr.delivery_location = in_delivery_location;
IF(in_quantity <= 9) THEN
out_total = val1;
END IF;
IF(in_quantity > 9) THEN
out_total = val2;
END IF;
END$$
DELIMITER ;
When executed it gives following 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 '= val1;
END IF;
IF(in_quantity > 9) THEN
out_total = val2' at line 16
beacause any value assign to a variable in mysql , we use SET keyword.
so change only in procedure
SET out_total = val1;
SET out_total = val1;
DELIMITER $$
CREATE PROCEDURE freight_calc(
IN in_delivery_location VARCHAR(100),
IN in_category_id INT(11),
IN in_quantity INT(11),
OUT out_total DECIMAL(10,2)
)
BEGIN
DECLARE val1 DECIMAL(10,2);
DECLARE val2 DECIMAL(10,2);
SELECT col1,col2 INTO val1, val2
FROM `freight_rules` fr
WHERE fr.category_id = in_category_id AND fr.delivery_location = in_delivery_location;
IF(in_quantity <= 9) THEN
SET out_total = val1;
END IF;
IF(in_quantity > 9) THEN
SET out_total = val1;
END IF;
END$$
DELIMITER ;

Error in case in mysql for creating procedure

create procedure temp (in empId int)
begin
declare emptype varchar;
select emptype = qoute(emptype) from dms_document where id = empid;
select emptype
case
when emptype = 'P' then
select doctype from dms_report where pilot = 1
else
select 'No Documents required'
end case
end;
This is my query i am creating procedure in MySQL, i am getting error in case statement please hlep me why this error is coming how declare case statement why error is coming in workbench for creating procdure
You are missed comma.
CREATE PROCEDURE temp (IN empId INT)
BEGIN
DECLARE emptype VARCHAR;
SELECT emptype = qoute(emptype) FROM dms_document WHERE id = empid;
SELECT emptype,
CASE
WHEN emptype = 'P' THEN doctype;
ELSE 'No Documents required';
END CASE ;
FROM dms_report WHERE pilot = 1
End;
Here I formatted your procedure Use delimiter
DELIMITER //
CREATE PROCEDURE temp ( empId INT)
BEGIN
DECLARE var_etype VARCHAR(36);
SELECT
emptype = QOUTE(emptype)
FROM
dms_document
WHERE
id = empid;
SELECT
emptype,
CASE
WHEN emptype = 'P' THEN doctype
ELSE 'No Documents required'
END
FROM
dms_report
WHERE
pilot = 1;
End//
DELIMITER ;

Conversion fail from MSSQL to MySQL

I encountered a problem when trying to convert MSSQL code to MySQL.
My MSSQL code is:
CREATE PROCEDURE ProductGroupGenerationSettingsCheck
AS
declare #id bit , #result varchar(50);
SELECT #id = automaticProductIdGeneration
FROM tbl_Settings--)
if(#id =0)
begin
set #result='false'
end
else if (#id =1)
begin
set #result='true'
end
select #result
My MySQL code is:
delimiter //
create procedure ProductGroupGenerationSettingsCheck(p_id tinyint(1),p_result varchar(50))
begin
select p_id = automaticProductIdGeneration from tbl_Settings ;
if(p_id = 0)
begin
set p_result = 'false' ;
end
else if (p_id = 1)
begin
set p_result = 'true' ;
end
select p_result as 'result' ;
end //
delimiter ;
the error I get 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 'begin set p_result = 'false' ; end else if (p_id = 1) begin set p_re' at line 5
What is wrong in my code?
delimiter //
create procedure ProductGroupGenerationSettingsCheck(IN p_id tinyint(1),IN p_result varchar(50)) /*specify what type of parameter it is, IN / OUT / INOUT*/
begin
select p_id := automaticProductIdGeneration from tbl_Settings ; /*use assignment operator := instead of comparison =*/
if(p_id = 0) then /*missing a then here*/
begin
set p_result = 'false' ;
/*don't end the if, when you still have an else if condition*/
else if (p_id = 1) then /*missing a then again*/
begin
set p_result = 'true' ;
end
end if; /*missing an if here*/
select p_result as 'result' ; /*you could also use an OUT parameter for this...anyway...*/
end //
delimiter ;
manual entry if statement
There are one error in the query at the forth line. Yo must use the next query.
select automaticProductIdGeneration into p_id from tbl_Settings;
It could be usefull if you post the entire error message. It shows you where is exactly the first problem.

MySQL procedure loop error

When running my query I am getting this:
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 '' at line 3
SQL:
DROP PROCEDURE IF EXISTS insertPromos;
CREATE PROCEDURE insertPromos()
BEGIN
DECLARE int_val INT DEFAULT 0;
insertPromosLoop : LOOP
IF (int_val = 501) THEN
LEAVE insertPromosLoop;
END IF;
INSERT INTO `promo_code` (`code`, `valid_from` ,`valid_to` , `free_period`)VALUES (CONCAT('PROMO', int_val), '2013-10-10', '2013-11-10', 'P1M');
SET int_val = int_val +1;
END LOOP;
END;
CALL insertPromos();
You need to define a new delimiter
delimiter |
DROP PROCEDURE IF EXISTS insertPromos |
CREATE PROCEDURE insertPromos()
BEGIN
DECLARE int_val INT DEFAULT 0;
insertPromosLoop : LOOP
IF (int_val = 501) THEN
LEAVE insertPromosLoop;
END IF;
INSERT INTO `promo_code` (`code`, `valid_from` ,`valid_to` , `free_period`)VALUES (CONCAT('PROMO', int_val), '2013-10-10', '2013-11-10', 'P1M');
SET int_val = int_val +1;
END LOOP;
END
|
delimiter ;
CALL insertPromos();
Otherwise the procedure definition would end at the first ; which would not be correct.