ERROR 1064: SQL syntax - mysql

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;
...

Related

SQL - Triple nested while insert

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.

My procedure gives error when I run

My procedure gives error when I run. I couldn't find where is mistake ?
CREATE PROCEDURE testing ()
BEGIN
DECLARE i INT;
DECLARE vSite VARCHAR(100);
set #i = 1;
BEGIN WHILE #i <= 5 DO
SET #vSite = #vSite + CONCAT('LINE '+#i+', ');
SET #i = #i + 1;
END WHILE
SELECT #vSite;
END
/* Error
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 'SELECT #vSite; END' at line 10
*/
/* required output given below
LINE 1, LINE 2, LINE 3, LINE 4, LINE 5,
*/
Resolved By My Self
delimiter //
CREATE procedure while_examples()
wholeblock:BEGIN
declare str VARCHAR(255) default '';
declare x INT default 0;
SET x = 1;
WHILE x <= 5 DO
SET str = CONCAT(str,'LINE',x,',');
SET x = x + 1;
END WHILE;
select str;
END//
CALL while_examples();

Error Code : 1064 You have an error in your SQL syntax (the new One)

I got this error message from my MySQL code. But I don't know where is my fault..
Here's my code:
DELIMITER $$
USE `kp`$$
DROP PROCEDURE IF EXISTS `getAllUmurPegawai`$$
CREATE DEFINER=`root`#`localhost` PROCEDURE `getAllUmurPegawai`()
BEGIN
DECLARE currdate INT;
DECLARE birthdate INT;
DECLARE numRows INT;
DECLARE numIteration INT;
DECLARE tempMonth INT;
SET numRows = SELECT COUNT(*) FROM pegawai;
SET numIteration = 1;
WHILE numIteration <= numRows DO
SET currdate = (SELECT EXTRACT(YEAR FROM CURRENT_DATE));
SET birthdate = (SELECT EXTRACT(YEAR FROM (SELECT TGL_LAHIR FROM pegawai WHERE INDEXING = numIteration)));
SET umur = currdate - birthdate;
SET tempMonth = SELECT EXTRACT(MONTH FROM (SELECT TGL_LAHIR FROM pegawai WHERE INDEXING = numIteration));
IF umur < 56 THEN
INSERT INTO pegawai(STATUS_PEGAWAI,BULAN_PENSIUN(JIKA_SUDAH)) VALUES('Belum Pensiun',0);
ELSE
INSERT INTO pegawai(STATUS_PEGAWAI,BULAN_PENSIUN(JIKA_SUDAH)) VALUES('Pensiun',tempMonth);
END IF;
SET numIteration = numIteration + 1;
END WHILE;
END$$
DELIMITER ;
and i get this error message:
Query : CREATE DEFINER=`root`#`localhost` PROCEDURE `getAllUmurPegawai`() BEGIN DECLARE currdate INT; DECLARE birthdate INT; DECLARE num...
Error Code : 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 'SELECT COUNT(*) FROM pegawai;
SET numIteration = 1;
WHILE numIteration <= numR' at line 8
How can i solve this problem? Best Answer will be appreciated :))
To use a SELECT query as an expression you have to put it in parentheses:
SET numRows = (SELECT COUNT(*) FROM pegawai);
You did it correctly when you were setting currDate and birthdate, but you forgot the parentheses on this line.

SQL: Create table with term names (String containing years)

I would like to create a table (two columns, first is auto incremental) which contains the term names, e.g. "SS 2000" and "WS 2000/2001" (for summer and winter term).
I tried the following:
CREATE PROCEDURE create_terms()
BEGIN
Declare #YearEnd integer;
SET #YearEnd = 2014;
Declare #YearFrom integer = #YearEnd - 100;
Declare #Term varchar = '';
while #YearFrom < #YearEnd Begin
#Term = concat('SS ', #YearFrom);
Insert into terms (term) VALUES #Term;
Set #YearFrom = #YearFrom + 1;
End
END
but I already get an error in line 3: SQL query:
CREATE PROCEDURE create_terms()
BEGIN
Declare #YearEnd integer;
MySQL said: Documentation #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 '#YearEnd integer' at line 3
After reading the comments from Abishek and Stuart, I tried the following:
DELIMITER $$
DROP PROCEDURE IF EXISTS create_terms$$
CREATE PROCEDURE create_terms()
BEGIN
DECLARE YearEnd INT;
Declare YearFrom INT;
Declare Term varchar(10);
SET YearEnd = 2014;
SET YearFrom = YearEnd - 100;
SET Term= '';
WHILE (YearFrom < YearEnd) DO
SET Term = concat('SS ', YearFrom);
Insert into terms (term) VALUES (Term);
Set YearFrom = YearFrom + 1;
END WHILE;
END;
DELIMITER ;
This results in just the DROP PROCEDURE command being successfully processed. Even when removing this line and changing the first lines to:
DELIMITER $$
CREATE PROCEDURE create_terms()$$
BEGIN
it doesn't work, the SQl console just writes "ERROR" and that's it.... :(
There are quite a bunch of errors here
You should use procedure variables
The Syntax for insert is INSERT INTO Table(columns) VALUES(values);
You probably also want to insert the term, not the year end
Your syntax for while is wrong
CREATE PROCEDURE create_terms()
BEGIN
DECLARE YearEnd INT;
Declare YearFrom INT;
Declare Term varchar(10);
SET YearEnd = 2014;
SET YearFrom = YearEnd - 100;
SET Term= '';
WHILE (YearFrom < YearEnd) DO
SET Term = concat('SS ', YearFrom);
Insert into terms (term) VALUES (Term);
Set YearFrom = YearFrom + 1;
END WHILE;
END
Fiddle here
All variable staring # are user variables and need not to be declared, procedures has their local variables not prefix with #, try like this:
DELIMITER $$
DROP PROCEDURE IF EXISTS create_terms$$
CREATE PROCEDURE create_terms()
BEGIN
Declare YearEnd integer;
SET YearEnd = 2014;
Declare YearFrom integer = YearEnd - 100;
Declare Term varchar = '';
while YearFrom < YearEnd Begin
Term = concat('SS ', YearFrom);
Insert into terms (term) VALUES YearFrom;
Set YearFrom = YearFrom + 1;
End;
END;
DELIMITER ;
Here is help

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 ;