SQL - Triple nested while insert - mysql

I have this code for insert rows to table. I have three while nesthed themself, but this code give me
error #1064 - bad syntax close
BEGIN
WHILE p <= 5
BEGIN
WHILE ra <= 40
' on line 7.
What is wrong with this code?
DELIMITER $$
CREATE PROCEDURE proc()
BEGIN
DECLARE r int DEFAULT 1;
DECLARE p int DEFAULT 1;
DECLARE ra int DEFAULT 1;
WHILE r <= 8 DO
WHILE p <= 5 DO
WHILE ra <= 40 DO
INSERT INTO tabulka (REGAL,POLICE,RADA) VALUES(r,p,ra);
SET ra = ra + 1;
END WHILE;
SET p = p + 1;
END WHILE;
SET r = r + 1;
END WHILE;
END$$
DELIMITER ;
CALL proc();
EDIT: Now it generates only one loop:

MySQL uses WHILE DO/END WHILE for it syntax. So the stored procedure should look like this:
CREATE PROCEDURE proc()
BEGIN
DECLARE r int DEFAULT 1;
DECLARE p int DEFAULT 1;
DECLARE ra int DEFAULT 1;
WHILE r <= 8 DO
WHILE p <= 5 DO
WHILE ra <= 40 DO
INSERT INTO tabulka (REGAL,POLICE,RADA) VALUES(r,p,ra);
SET ra = ra + 1;
END WHILE;
SET p = p + 1;
END WHILE;
SET r = r + 1;
END WHILE;
END;
Here is a little rextester.

Okay, my mistake. I forgot to reset variables to 1 after inside loops was done. Thanks for help.

Related

MySQL Set Variables in While-Loop with If-Condition

I am working on a procedure which is doing the following:
With a cursor declared I want to go through all entries of a table to find entries where the given period is covering the one from the cursor.
Example: a student A went to school at grade x from date 3 to date 6. Let's find all who studied with him at least one time for the same class.
The while-loop would be applied to the same table. But I want to just find out if there is at least one entry or not. So the while loop should stop if the first entry appears.
Example: A student B could have visited the same class as A did. But for a year he has been in another class. Just for example.
And here is my problem. I have two variables to set and I get a syntax error.
DELIMITER $$
DROP PROCEDURE IF EXISTS coworkers$$
CREATE PROCEDURE coworkers(
IN gus INT,
IN rus INT,
OUT gto INT
)
BEGIN
DECLARE recCID INT;
DECLARE recSDT DATE;
DECLARE recEDT DATE;
DECLARE done INT DEFAULT FALSE;
DECLARE ctr INT;
DECLARE cwrk CURSOR FOR
SELECT comp_id, start_date, end_date FROM skill_cv_test WHERE usr_id = rus;
DECLARE CONTINUE HANDLER
FOR NOT FOUND
SET done = TRUE;
OPEN cwrk;
SET ctr = 0;
loop_cwrk: WHILE(ctr<1) DO
FETCH cwrk INTO recCID, recSDT, recEDT;
IF EXISTS
(SELECT *
FROM skill_cv_test AS m
WHERE m.usr_Id = gus AND m.usr_id != rus AND (m.start_date < recSDT OR m.end_date <= recEDT) AND m.comp_id = recCID)
THEN
SET ctr = 1,
SET gto = 1;
IF done THEN
LEAVE loop_cwrk;
END IF;
END WHILE loop_cwrk;
CLOSE cwrk;
end $$
delimiter ;
CALL coworkers(2,1,#gto);
I have tried different formats for the section THEN SET... which is the problem.
Here is the error code:
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 ''ctr' := 1,
SET 'gto' ;= 1;
Here you can see what else I have tried (among others). Probably it's a totally wrong approach.
Btw: the Select within the loop is only a placeholder to get the whole procedure work.
But now, what am I doing wrong?
Thanks in advance.
SOLUTION
DELIMITER $$
DROP PROCEDURE IF EXISTS coworkers$$
CREATE PROCEDURE coworkers(
IN gus INT,
IN rus INT,
OUT gto INT
)
BEGIN
DECLARE recCID INT;
DECLARE recSDT DATE;
DECLARE recEDT DATE;
DECLARE done INT DEFAULT FALSE;
DECLARE ctr INT;
DECLARE cwrk CURSOR FOR
SELECT comp_id, start_date, end_date FROM skill_cv_test WHERE usr_id = rus;
DECLARE CONTINUE HANDLER
FOR NOT FOUND
SET done = TRUE;
OPEN cwrk;
SET ctr = 0;
loop_cwrk: WHILE(ctr<1) DO
FETCH cwrk INTO recCID, recSDT, recEDT;
IF EXISTS
(SELECT *
FROM skill_cv_test AS m
WHERE m.usr_Id = gus AND m.usr_id != rus AND (m.start_date < recSDT
OR m.end_date <= recEDT) AND m.comp_id = recCID)
THEN
SET ctr = 1;
SET gto = 1;
END IF;
IF done THEN
LEAVE loop_cwrk;
END IF;
END WHILE loop_cwrk;
CLOSE cwrk;
end $$
delimiter ;
CALL coworkers(2,1,#gto);
You need add END IF for your first IF
IF EXISTS
(SELECT *
FROM skill_cv_test AS m
WHERE m.usr_Id = gus AND m.usr_id != rus AND (m.start_date < recSDT
OR m.end_date <= recEDT) AND m.comp_id = recCID)
THEN
SET ctr = 1;
SET gto = 1;
END IF; -- Add END IF here
IF done THEN
LEAVE loop_cwrk;
END IF;

ERROR 1064: SQL syntax

i want to create a procedure in MySQL/MariaDB, but I get a syntax error which I do not understand.
DELIMITER $$
DROP PROCEDURE IF EXISTS proc_loop$$;
CREATE PROCEDURE proc_loop()
BEGIN
DECLARE i INT;
DECLARE j INT;
DECLARE n INT;
DECLARE c VARCHAR(3);
SET i = 1;
SET j = 1;
SELECT COUNT(*) INTO n FROM AnswerSets;
WHILE i < n DO
WHILE j < 89 DO
SELECT CONCAT("Q", j) INTO c;
INSERT INTO T_ANSWER_SET (U_ID, Q_ID, ANSWER) SELECT i, j, c FROM AnswerSets WHERE Id = i;
SET j = j + 1;
END WHILE;
SET i = i + 1;
END WHILE;
END$$;
DELIMITER ;
My error message:
ERROR 1064 (42000) at line 16: 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 ';
CREATE PROCEDURE proc_loop()
BEGIN
DECLARE i INT;
DECLARE j INT;
DECLARE n ' at line 1
Thanks in advance for help.
Try This:
DROP PROCEDURE IF EXISTS proc_loop;
DELIMITER $$
CREATE PROCEDURE `proc_loop`()
BEGIN
DECLARE i INT;
DECLARE j INT;
DECLARE n INT;
DECLARE c VARCHAR(3);
SET i = 1;
SET j = 1;
SELECT COUNT(*) INTO n FROM AnswerSets;
WHILE i < n DO
WHILE j < 89 DO
SELECT CONCAT("Q", j) INTO c;
INSERT INTO T_ANSWER_SET (U_ID, Q_ID, ANSWER) SELECT i, j, c FROM AnswerSets WHERE Id = i;
SET j = j + 1;
END WHILE;
SET i = i + 1;
END WHILE;
END$$
DELIMITER ;
Should that not work, as a means of debugging try removing all of the proc content in favor of something trivial like select 1; and see if the error persists. I've seen this error reported incorrectly before due to a slight syntax error in the proc body.
To debug the insert, temporarily comment out the insert into t_answer_set:
...
SELECT CONCAT("Q", j) INTO c;
-- INSERT INTO T_ANSWER_SET (U_ID, Q_ID, ANSWER)
SELECT i, j, c FROM AnswerSets WHERE Id = i;
...

count total even and odd number between 20-50 with mysqll procedure

i'm having hard time with my homework , because im beginner using mysql procedure.
to search total numbers odd and even between 20-50 with looping.
i want to make output like this using mysql procedure to call:
total_odd_numbers :15
total_even_numbers:16
i tried for sum up for even numbers like this :
`DELIMITER $$
CREATE DEFINER=`root`#`localhost` PROCEDURE `genap`()
BEGIN
DECLARE x INT;
DECLARE str VARCHAR(255);
declare total int;
SET x = 20;
SET str = '';
set total = 0;
loop_label: LOOP
IF x = 50 THEN
LEAVE loop_label;
END IF;
SET x =x+1;
IF (x mod 2) THEN
ITERATE loop_label;
ELSE
SET str = CONCAT(str,x,',');
SET total= total+x;
END IF;
END LOOP;
SELECT sum(total);
END`
Try this procedure:
DROP PROCEDURE IF EXISTS ShowOddEvesBetween;
DELIMITER ;;
CREATE PROCEDURE ShowOddEvesBetween(IN fromNum INT, IN toNum INT)
BEGIN
DECLARE odds INT DEFAULT 0;
DECLARE evens INT DEFAULT 0;
DECLARE x INT;
SET x = fromNum;
WHILE x <= toNum DO
IF x % 2 THEN
SET odds = odds + 1;
ELSE
SET evens = evens + 1;
END IF;
SET x = x + 1;
END WHILE;
SELECT CONCAT("total_odd_numbers: ", odds, ", total_even_numbers: ", evens) AS "Odds & Evens";
END;
;;
Then call it
CALL ShowOddEvesBetween(20, 50);
Will output what you want
MariaDB [test]> CALL ShowOddEvesBetween(20, 50);
+-----------------------------------------------+
| Odds & Evens |
+-----------------------------------------------+
| total_odd_numbers: 15, total_even_numbers: 16 |
+-----------------------------------------------+
1 row in set (0.00 sec)

cursor error syntax in mysql

i have such procedure but all the time i want to execute it i get syntax error near declaring variables... can anybody tell me what i am doint wrong
i have two tables one for questions and one for answers and excel file to migrate data to this tables.
data in excel looks like this:
ID N TITLE
3 99500 question1
4
5 answer1
6 X answer2
7 answer3
DELIMITER $$
create Procedure proc_answermigration()
begin
DECLARE #i,#n,#q,#ind,#pt varchar default '';
DECLARE #done,#aord int default 0;
DECLARE cur cursor for select ID, N, question, from test.qustionmigration;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET #done = 1;
open cur;
read_loop: Loop
fetch cur into i,n,q;
IF done THEN
LEAVE read_loop;
END IF;
if n <> '' or n <> 'X'
then
select #ind = iq.question_id, #pt = iq.points from test.qustionmigration qm
inner join ilias.qpl_questions iq on qm.question = iq.question_text
where question = q
else
insert into qpl_a_sc
(
answer_id,
question_fi,
answertext,
points,
aorder,
tstamp
)
select (select sequence from qpl_a_sc_seq),
ind,
question,
pt,
aord,
'1342884200'
from test.qustionmigration
end if;
update qpl_a_sc_seq
set sequence = sequence + 1;
if #aord = 0 then set #aord = 1;
elseif #aord = 1 then set #aord = 2;
else set #aord = 0;
end loop;
CLOSE cur;
end$$
DELIMITER ;
i corrected some statements but still it has syntax error saying:
#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 'LOOP; close cur; end' at line 52
DELIMITER $$
create Procedure proc_answermigration()
begin
DECLARE i,n,q,ind,pt varchar(500) default '';
DECLARE done,aord,c int default 0;
DECLARE cur cursor for select ID, N, question from test.qustionmigration;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
open cur;
read_loop: LOOP
fetch cur into i,n,q;
IF n <> '' or n <> 'X'
then
select ind = iq.question_id, pt = iq.points from test.qustionmigration qm
inner join ilias.qpl_questions iq on qm.question = iq.question_text
where question = q;
else
insert into qpl_a_sc
(
answer_id,
question_fi,
answertext,
points,
aorder,
tstamp
)
select (select sequence from qpl_a_sc_seq),
ind,
question,
pt,
aord,
'1342884200'
from test.qustionmigration;
end IF;
update qpl_a_sc_seq
set sequence = sequence + 1;
if aord = 0 then set aord = 1;
elseif aord = 1 then set aord = 2;
else set aord = 0;
set c = c + 1;
IF done = 1 THEN
LEAVE read_loop;
END IF;
END LOOP;
close cur;
end$$
DELIMITER ;
Your last IF ELSE block is missing its END IF:
if #aord = 0 then set #aord = 1;
elseif #aord = 1 then set #aord = 2;
else set #aord = 0;
/* END IF either belongs here or after following statements, depending on your intended logic */
/* Either way, this block is unclosed when you close the loop */
end if
When the parser reads the END LOOP;, it is looking for the END IF, and reports a syntax error at LOOP.
You can't DECLARE user variables (those beginning with a #). Just SET them, or else use local variables (not beginning with a #).

Nested loop in mysql stored procedure

I am having a simple nested while loop in stored procedure , but it's giving an error.
The loop does not nothing great, just for learning purpose i created two loops.
delimiter $$
create procedure getSum(in input int , out output int)
begin
set output = 0;
while input >= 1 do
declare tmp int default 1;
while tmp <= 5 do
set output = output + input ;
set tmp = tmp + 1;
end while ;
set input = input - 1 ;
end while;
end $$
delimiter ;
Below is the 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 'declare tmp int default 1; while tmp <= 5 do set output = output + inpu' at line 7
Thanks.
Try this:
delimiter $$
create procedure getSum(in input int , out output int)
begin
declare tmp int default 1;
set output = 0;
while input >= 1 do
set tmp = 1;
while tmp <= 5 do
set output = output + input ;
set tmp = tmp + 1;
end while ;
set input = input - 1 ;
end while;
end $$
delimiter ;