MySQL 5.7 Cannot find error in simple WHILE statement - mysql

I am trying to use a WHILE loop in MySQL v5.7 and keep getting a syntax error. I haven't been able to identify the problem. The syntax looks correct according to the documentation.
I found a thread here suggesting wrapping the statement in a DELIMITER, but this did not work either. The code is:
SET #counter = 1;
WHILE (#counter < 2) DO
SELECT #counter;
#counter = #counter + 1;
END WHILE
and the error message is:
ERROR 1064 (42000) at line 22: 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 (#counter < 2) DO
SELECT #counter' at line 1
What am I missing?

As far as I remember, you cannot use WHILE LOOP just like that. You have to put it inside a Stored Procedure like so:
CREATE PROCEDURE mysp()
BEGIN
DECLARE counter int DEFAULT 1;
WHILE counter < 2 DO
SET counter = counter + 1;
END WHILE;
END

Try the following code. It ran successfully on my MySQL 5.7
DELIMITER //
CREATE PROCEDURE mysp()
BEGIN
DECLARE counter INT;
SET counter = 1;
label1: WHILE counter < 2 DO
SELECT counter;
SET counter = counter + 1;
END WHILE label1;
END; //
DELIMITER ;

Related

MYSQL is not supporting a while loop containing more than one statement

I am trying to install a procedure in Mysql 5.6 launching the command from mysql workbench (version 6).
I get a sintax error with no apparent reason.
If I just remove one of the two assigments inside the while loop, then it would works.
How come i cannot put two statements inside the while?
DELIMITER //
CREATE PROCEDURE PROC (IN TABLE_ varchar(400))
BEGIN
DECLARE Statement varchar(400) DEFAULT "";
DECLARE i INTEGER DEFAULT 1;
DECLARE N INTEGER DEFAULT 2;
while i <= N do
set Statement = 'a';
set i = i+1;
end while;
END
//
DELIMITER ;
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 '= 'a'; set i = i+1; end while; END' at line
10

mysql stored procedure declaration throws error on execution

I"m continiously receiving this error when trying to create this stored procedure. I'm trying to write a procedure that splits a comma delimited string. Similar to explode. I feel I'm close.
This is the 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 'DECLARE start_pos, end_pos INT;
SET start_pos = 1;
SET end_pos = Locat' at line 6
I copied the logic from a SQL Server example and did my best translate it to MySql syntax.
This is the entire procedure from start to finish. I'm hoping a well trained eye can explain why I'm getting an error.
DELIMITER $$
CREATE procedure split_string (in p_string_to_split VARCHAR(255),in p_delimiter CHAR(1) )
BEGIN
DROP TEMPORARY TABLE IF EXISTS split_channel_ids;
CREATE TEMPORARY TABLE split_channel_ids (p_channel_id int);
DECLARE start_pos, end_pos INT;
SET start_pos = 1;
SET end_pos = Locate(p_delimiter, p_string_to_split);
WHILE (start_pos < CHAR_LENGTH(p_string_to_split) + 1) DO
IF (end_pos = 0) THEN
SET end_pos = CHAR_LENGTH(p_string_to_split) + 1;
END IF;
--- INSERT split_channel_ids (p_channel_id)
--- VALUES(SUBSTRING(p_string_to_split, start_pos, end_pos - start_pos)) ;
SET start_pos = end_pos + 1;
SET end_pos = Locate(p_delimiter, p_string_to_split, start_pos);
END WHILE;
-- select * from imob_users;
select * from split_channel_ids;
END $$
DELIMITER ;
DECLARE statements (in MySQL) must be at the beginning of their enclosing BEGIN...END block.
In MS SQL, they can be anywhere; but it is annoying there because they do not have block scope, they have procedure scope, so you can't "re-use" names in independent blocks.

MySQL Delcare causing error

I am trying to create a trigger that marks items as deleted when they are inserted into the database.
Sadly I can't get my DECLARE to stop erroring, I have looked at the DECLARE docs and also at a few examples but I must be missing something.
The query I have so far is:
CREATE TRIGGER set_deleted BEFORE INSERT ON customercontact
FOR EACH ROW
BEGIN
DECLARE numrow INT; /* line 4 */
SELECT COUNT(*)
INTO numrow
FROM orders
WHERE NEW.order_id = 1;
if numrow >= 1 THEN
SET new.deleted = 1;
END IF;
END
The error message is showing:
#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 4
Thanks for your help and preventing me from defenestrating myself!
Try this:
DELIMITER $$
CREATE TRIGGER set_deleted BEFORE INSERT ON customercontact
FOR EACH ROW
BEGIN
DECLARE numrow INT; /* line 4 */
SELECT COUNT(*)
INTO numrow
FROM orders
WHERE NEW.order_id = 1;
if numrow >= 1 THEN
SET new.deleted = 1;
END IF;
END$$
DELIMITER ;
You need to change the delimiter when you create TRIGGER or STORED PROCEDURE.
By default, MySQL itself recognizes the semicolon as a statement delimiter, so you must redefine the delimiter temporarily to cause MySQL to pass the entire stored program definition to the server. Otherwise, MySQL breaks CREATE TRIGGER, before it reaches the END statement (on the first semicolon, which, in your case, is DECLARE statement).
You can see the documentation for more details:
http://dev.mysql.com/doc/refman/5.5/en/stored-programs-defining.html

MySQL stored procedure syntax pain

I am trying to create a simple Stored procedure that allows me to conduct mass inserts, However I am running into syntactical troubles and unable to figure out where what's going wrong, despite comparing my procedure syntax to existing examples, and it seems to be correct.
CREATE PROCEDURE populateUserTable()
BEGIN
DECLARE counter int(10);
SET counter = 1;
WHILE counter < 101 DO
INSERT INTO user(userid) values(counter);
SET counter = counter + 1
END WHILE;
END
Upon running, MYSQL states:
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
and highlits this guy:
CREATE PROCEDURE populateUserTable( ) BEGIN DECLARE counter INT( 10 ) ;
What's up here?
Have you used
DELIMITER $$
At the start?
Try
DELIMITER $$
CREATE PROCEDURE populateUserTable()
BEGIN
DECLARE counter int(10);
SET counter = 1;
WHILE counter < 101 DO
INSERT INTO user(userid) values(counter);
SET counter = counter + 1
END WHILE;
END $$
DELIMITER ;

syntax error for mysql declaration of variable

CREATE PROCEDURE dorepeat(IN p1 INT)
BEGIN
DECLARE x INT DEFAULT 0;
REPEAT SET x = x + 1; UNTIL x > p1 END REPEAT;
END
I get an syntax 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
But for me, everything seems to be correct. i really don't have any clue! can anybody help?
thanks
You need to temporarily change the delimiter so the MySQL client doesn't think you're done with your statement when it sees the semicolon on line 3:
DELIMITER //
CREATE PROCEDURE dorepeat(IN p1 INT)
BEGIN
DECLARE x INT DEFAULT 0;
REPEAT SET x = x + 1; UNTIL x > p1 END REPEAT;
END//
DELIMITER ;
Remove the DECLARE, you should be able to just do this:
SET #x = 0;
Also, variables need to be prefixed with the # symbol