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 ;
Related
The official documentation of MySQL says that using label for LOOP statement is optional. But when I used LOOP statement in a stored procedure without a label, an error occurred. The SQL script file is as follows:
DELIMITER $$
DROP PROCEDURE IF EXISTS loop_demo $$
CREATE PROCEDURE loop_demo()
BEGIN
DECLARE i INT;
DECLARE sum INT;
SET i = 0;
SET sum = 0;
LOOP
IF i > 10 THEN LEAVE ;
END IF;
SET i = i + 1;
IF (i MOD 2) THEN ITERATE ;
ELSE
SET sum = sum + i;
END IF;
END LOOP;
SELECT sum;
END $$
DELIMITER ;
CALL loop_demo();
This is a simple loop which finds the sum of even no.s less than or equal to 10. But when I run the program the following error message generated:
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 ';
END IF;
SET i = i + 1;
IF (i MOD 2) THEN ITERATE ;
ELSE
' at line 8
What could have went wrong?
Your are basically missing a label for the loop. It should be declared when the loop starts, and referred to in the ITERATE, LEAVE and END LOOP statements. I would also recommend not naming your variable sum, since it conflicts with a SQL keyword.
Consider:
DELIMITER $$
CREATE PROCEDURE loop_demo()
BEGIN
DECLARE i INT;
DECLARE sm INT;
SET i = 0;
SET sm = 0;
lbl: LOOP
IF i > 10 THEN LEAVE lbl;
END IF;
SET i = i + 1;
IF (i MOD 2) THEN ITERATE lbl;
ELSE
SET sm = sm + i;
END IF;
END LOOP lbl;
SELECT sm;
END $$
DELIMITER ;
Demo on DB Fiddle
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. 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();
I have created mysql function to split the string in rows using temporary table, it gives correct in temp table, but not when i call the function it returns 1 as output.
My code is as below
DELIMITER $$
DROP FUNCTION IF EXISTS `split`$$
CREATE FUNCTION `split`(`String` VARCHAR(8000), `Delimiter` CHAR(1)) RETURNS VARCHAR(8000) CHARSET latin1
BEGIN
DECLARE idx INT;
DECLARE slice VARCHAR(8000);
DECLARE result INT DEFAULT 0;
DROP TEMPORARY TABLE IF EXISTS tmp;
CREATE TEMPORARY TABLE `tmp` (items VARCHAR(8000));
-- set #tmptab = tmp;
SET idx = 1;
label: WHILE LENGTH(`String`)<1 OR `String` IS NULL DO
LEAVE label;
END WHILE;
loop_lable: WHILE (idx<>0) DO
SET idx = LOCATE(`Delimiter`,`String`);
IF (idx<>0) THEN
SET slice = LEFT(`String`,idx - 1);
ELSE
SET slice = `String`;
END IF;
IF(LENGTH(slice)>0) THEN
INSERT INTO tmp(Items) VALUES(slice) ;
SET result = 1;
END IF;
SET `String` = RIGHT(`String`,LENGTH(`String`) - idx);
IF LENGTH(`String`) = 0 THEN
LEAVE loop_lable;
END IF;
END WHILE;
RETURN result;
END$$
DELIMITER ;
I found this function for capitalizing the first letter of words in a string, but it is giving me a syntax error on line 8 when I try to run it in SQL in PHPMyadmin. Can anyone help me sort out what the problem is?
Here is the code:
CREATE FUNCTION CAP_FIRST (input VARCHAR(255))
RETURNS VARCHAR(255)
DETERMINISTIC
BEGIN
DECLARE len INT;
DECLARE i INT;
SET len = CHAR_LENGTH(input);
SET input = LOWER(input);
SET i = 0;
WHILE (i < len) DO
IF (MID(input,i,1) = ' ' OR i = 0) THEN
IF (i < len) THEN
SET input = CONCAT(
LEFT(input,i),
UPPER(MID(input,i + 1,1)),
RIGHT(input,len - i - 1)
);
END IF;
END IF;
SET i = i + 1;
END WHILE;
RETURN input;
END;
And this is the error:
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 '' at line 8
The delimiter signals the DB engine the end of your statement. Normally it is ;. But that would end the stored procedure at the first ;. And its definition would be incomplete.
You can change the delimiter and add it to the end of your procedure. After that change the delimiter back to ;
delimiter |
CREATE FUNCTION CAP_FIRST (input VARCHAR(255))
...
END;
|
delimiter ;