Conversion fail from MSSQL to MySQL - 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.

Related

MySQL Error 1064 when create function

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 ;

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 ;

Syntax error when creating a MySQL trigger

I have some problems trying to do a trigger.
Here is my MySQL query for the trigger:
delimiter //
CREATE TRIGGER `aggiornaProduzione`
BEFORE UPDATE ON `strutture` FOR EACH ROW
BEGIN
DECLARE temp bigint;
IF ( tipo=1/*mercato*/ AND old.livello <> new.livello )
THEN (
SELECT round(2*livello*livello + 13.8*livello)
FROM strutture WHERE tipo=1 AND city=old.city INTO temp;
WHILE (temp%6<>0) temp=temp+1;
END WHILE;
UPDATE strutture SET produzione=temp WHERE city=new.city AND tipo=1;
)
END IF;
END //
And the error 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 '; WHILE (temp%6<>0) temp=temp+1; END WHILE; UPDATE strutture SET ' at line 8
Does someone have an idea why I'm getting this error?
Remove the parentheses around your THEN clause. Also your WHILE clause is syntactically incorrect:
delimiter //
CREATE TRIGGER `aggiornaProduzione`
BEFORE UPDATE ON `strutture` FOR EACH ROW
BEGIN
DECLARE temp bigint;
IF ( tipo=1/*mercato*/ AND old.livello <> new.livello )
THEN
SELECT round(2*livello*livello + 13.8*livello)
FROM strutture WHERE tipo=1 AND city=old.city INTO temp;
WHILE temp%6<>0 DO
SET temp=temp+1;
END WHILE;
UPDATE strutture SET produzione=temp WHERE city=new.city AND tipo=1;
END IF;
END //

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.

Unable to create UDF in my sql

Unable to create UDF. Am getting below error at the time of creation
Error: 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 ';
CREATE FUNCTION `seat_count`(seatno varchar(250))
RETURNs varchar(250)
BEG' at line 1
code: http://pastebin.com/uf4DkXZh
code:
CREATE FUNCTION `seat_count`(seatno VARCHAR(250))
RETURNS VARCHAR(250)
BEGIN
SET #str= REPLACE(seatno, ',', '');
SET #stcnt=CHAR_LENGTH(str);
SET #x=1;
SET #result;
WHILE (#x <= #stcnt)
DO
SET #st=SUBSTRING_INDEX(#str,',',X);
IF ((LENGTH(#seatno) - LENGTH(REPLACE(#seatno, st, '')))/LENGTH(#st)=1)
THEN SET #result=CONCAT(#st,#result);
END IF;
SET #x=#x+1;
END WHILE;
RETURN #result;
END$$
When you develop a stored procedure in MySQL, you can pass the input parameters and declare the local variables, the local variable are not prepended with any prefixes unlike user-defined variable which are prepended with #. For detail procedure vs user-defined variable
DELIMITER $$
CREATE FUNCTION `SEAT_COUNT`(`seatno` VARCHAR(250)) RETURNS VARCHAR(250)
DETERMINISTIC
BEGIN
DECLARE result VARCHAR(250);
SET #str = REPLACE(seatno, ',', '');
SET #stcnt = CHAR_LENGTH(#str);
SET #x = 1;
WHILE (#x <= #stcnt)
DO
SET #st = SUBSTRING_INDEX(#str,',',X);
IF ((LENGTH(#seatno) - LENGTH(REPLACE(#seatno, st, '')))/LENGTH(#st)=1)
THEN SET result = CONCAT(#st, #result);
END IF;
SET #x = #x+1;
END WHILE;
RETURN result;
END$$
DELIMITER ;
You have defined the result variable as user-defined e.g SET #result instead of local variable e.g DECLARE result VARCHAR(250)
I hope this will work at your end too.