Flyway produces MySQL syntax error while raw script does not - mysql

For the following MySql script Flyway produces a MySQL syntax error while running the script directly in something like Navicat works fine. Can anyone tell me why that is?
CREATE PROCEDURE RegressionTest_Genealogy (OUT Success TINYINT)
BEGIN
DECLARE MetricVerification TINYINT;
SET Success = 0;
SELECT COUNT(MERTRICID) INTO MetricVerification FROM metrics_temp WHERE lft = 0 OR rgt = 0;
IF MetricVerification = 0 THEN
SET Success = 1;
END IF;
END

You probably need to issue a delimiter change first to make this work, as per default the delimiter is ; which is contained in your procedure body.

Try this
DELIMITER //
CREATE PROCEDURE RegressionTest_Genealogy (OUT Success TINYINT)
BEGIN
DECLARE MetricVerification TINYINT;
SET Success = 0;
SELECT COUNT(MERTRICID) INTO MetricVerification FROM metrics_temp WHERE lft = 0 OR rgt = 0;
IF MetricVerification = 0 THEN
SET Success = 1;
END IF;
END //
DELIMITER ;

Related

ERROR 1064 (42000): You have an error in your SQL syntax; check the manual mysql stored procedure error

I am just trying mysql stored procedure but I am getting the following error:
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 ';
DECLARE cur CURSOR for
select birth_date from employees;
' at line 3
Code:
delimiter //
create procedure getminmaxbirthdate(OUT minage date,OUT maxage date)
begin
DECLARE minsofar,maxsofar,curage,done;
DECLARE cur CURSOR for
select birth_date from employees;
set minsofar = 2040-12-12;
set maxsofar = 0;
set done = 0;
DECLARE continue handler for not found
set done = 1;
open cur;
while done = 0 do
fetch birthdate into curage;
if curage > maxsofar then
set maxsofar = curage;
end if;
if curage < minsofar then
set minsofar = curage;
end if;
end while;
close cur;
set minage = minsofar;
set maxage = maxsofar;
end //
delimiter ;
You have some bugs in your code
DECLARE without a tyoe is nocht valid
SET are only possible after DECLARE
and your CORSOR is cur not birthdate
you must check the datatypes for minsofar,maxsofar,curage,done
stored procedure:
delimiter //
create procedure getminmaxbirthdate(OUT minage date,OUT maxage date)
begin
DECLARE minsofar,maxsofar,curage,done VARCHAR(10);
DECLARE cur CURSOR for
select birth_date from employees;
DECLARE continue handler for not found
set done = 1;
set minsofar = 2040-12-12;
set maxsofar = 0;
set done = 0;
open cur;
while done = 0 do
fetch cur into curage;
if curage > maxsofar then
set maxsofar = curage;
end if;
if curage < minsofar then
set minsofar = curage;
end if;
end while;
close cur;
set minage = minsofar;
set maxage = maxsofar;
end //
delimiter ;
Is there a reason you are not using a simple query?
select min(birth_date), max(birth_date)
from employee;
This could be incorporated into a stored procedure, but that seems superfluous. Using a cursor is just a really, really, really bad practice -- even if you are learning SQL. Cursors should be avoided, except in the situations where they are necessary -- such as calling a stored procedure or invoking dynamic SQL for each row.
Your code also seems to be mixing up "ages" and "dates". It is a bit unclear what you really want to accomplish.

how to tell if a mysql insert is successful using while loop?

I am inserting the records in mysql database using while loop.I want to chcek the check atleast one record is inserted or not. I tried below code but ROW_COUNT() give me success, if the record is not inserted.
DELIMITER $$
DROP PROCEDURE IF EXISTS test$$
CREATE PROCEDURE test()
BEGIN
DECLARE count INT DEFAULT 0;
DECLARE res varchar(255);
WHILE count < 10 DO
/**Sql statement**/
SET count = count + 1;
END WHILE;
IF ROW_COUNT() > 0 THEN
SET res = 'success';
ELSE
SET res = 'failure';
END IF;
SELECT res;
END$$
DELIMITER ;

MySQL - UPDATE in batches using WHILE DO

Can someone give me pointers on what I'm doing wrong here?
I keep getting 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...
I'm trying to run an UPDATE command in small batches to prevent the table from being blocked (while running through 10 million records).
DELIMITER //
CREATE FUNCTION batch_update()
BEGIN
SET processed = 0;
SET eachbatch = 1000;
WHILE processed < 1000000 DO
UPDATE `users` SET `active` = 1 WHERE `active` = 0 LIMIT eachbatch;
-- Finish when no rows remaining
IF ROW_COUNT() = 0 THEN LEAVE;
END IF;
-- No infinite loop, thanks
SET processed = processed + eachbatch;
END WHILE;
END //
DELIMITER ;
CALL batch_update();
Edit: For anyone that needs a pointer, I got it working like this:
DROP FUNCTION IF EXISTS process_batch_rows;
DELIMITER //
CREATE FUNCTION process_batch_rows() RETURNS VARCHAR(10) DETERMINISTIC
BEGIN
DECLARE processed INT DEFAULT 0;
DECLARE eachbatch INT DEFAULT 1000;
my_loop: WHILE processed < 1000000
DO
UPDATE `users` SET `active` = 1 WHERE `active` = 0 LIMIT eachbatch;
IF ROW_COUNT() < 1 THEN
LEAVE my_loop;
END IF;
SET processed = processed + eachbatch;
END WHILE my_loop;
RETURN "Done";
END //
DELIMITER ;
SELECT process_batch_rows();
Try This one I have added comments wherever needed =>
DELIMITER //
-- You must specify return type
CREATE FUNCTION batch_update() RETURNS VARCHAR(10) DETERMINISTIC
BEGIN
-- you must declare variables
DECLARE processed int(10);
DECLARE eachbatch int(10);
SET processed = 0;
SET eachbatch = 1000;
-- needed my loop here so it can be used in leave
myloop: WHILE processed < 1000000 DO
UPDATE `users` SET `active` = 1 WHERE `active` = 0 LIMIT eachbatch;
-- Finish when no rows remaining
IF ROW_COUNT() = 0
THEN
LEAVE myloop;
END IF;
-- No infinite loop, thanks
SET processed = processed + eachbatch;
END WHILE;
return 'Done';
END //
DELIMITER ;

MySQL - Jump to "foo:Begin" in Stored Procedure

For example, I've code like this:
...
PROCEDURE procedure1(... some parameters ...)
BEGIN
DECLARE l_xxx VARCHAR(5);
SET l_xxx = 'ERROR';
IF l_xxx = 'ERROR' THEN
-- in this section, I want to call 'foo' process below
END IF;
foo:BEGIN
-- Some process
END;
END;
Can I call the error:BEGIN in a stored procedure..?
You could create another stored procedure, and call it instead of GOTO-like command.
Try to do it using IF-THEN statement with a help of user variable.
For example:
DECLARE l_xxx VARCHAR(5);
SET l_xxx = 'ERROR';
SET #a = NULL;
IF l_xxx = 'ERROR' THEN
SET #a = 1; -- Set error flag
END IF;
IF #a IS NULL THEN
-- Some process, when there were no errors.
END IF;
IF #a = 1 THEN
BEGIN
-- Some process
END;
END IF;

MYSQL function, is it Workbench or just noobish me?

I have been sitting with a stored procedure for MySQL for days now, it just won't work, so I thought I'd go back to basic and do a very simple function that checks if an item exists or not.
The problem I had on the first one was that it said END IF is invalid syntax on one of my IF clauses, but not the other two. The second one won't even recognize BEGIN as valid syntax...
Is it I that got everything wrong, or have I stumbled upon a MYSQL Workbench bug? I have Workbench 5.2 (latest version when I'm writing this) and this is the code:
DELIMITER $$
CREATE FUNCTION `filmsidan`.`f_lateornot` (movie_id INT)
BEGIN
DECLARE check_val INT;
DECLARE return_val INT;
SELECT stockId
FROM orders
WHERE stockId = movie_id
INTO check_val;
IF check_val <= 0
THEN
SET return_val = 1;
ELSE
SET return_val = 0;
END IF;
RETURN return_val;
END
to fix the "begin" syntax error, you have to declare a return value, like this:
CREATE FUNCTION `filmsidan`.`f_lateornot` (movie_id INT) RETURNS INT(11)
after doing that, Workbench won't return an error anymore ;o)
You have to specify the return value in signature as well delimiter at the end is missing. So, your function should look like
DELIMITER $$
CREATE FUNCTION `filmsidan`.`f_lateornot` (movie_id INT) RETURNS INT
BEGIN
DECLARE check_val INT;
DECLARE return_val INT;
SELECT stockId
FROM orders
WHERE stockId = movie_id
INTO check_val;
IF check_val <= 0
THEN
SET return_val = 1;
ELSE
SET return_val = 0;
END IF;
RETURN return_val;
END
$$
DELIMITER $$
CREATE FUNCTION `filmsidan`.`f_lateornot` (movie_id INT)
BEGIN
DECLARE check_val INT;
DECLARE return_val INT;
SELECT stockId
FROM orders
WHERE stockId = movie_id
INTO check_val;
IF check_val <= 0
THEN
SET return_val = 1;
ELSE
SET return_val = 0;
END IF;
RETURN return_val;
END
$$
DELIMITER ;
Add this last thing it works :
$$
DELIMITER ;
it means you are using ( ; ) this in function so for that reason we use it..see
and see also
MySQL - Trouble with creating user defined function (UDF)