syntax error for mysql declaration of variable - mysql

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

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 5.7 Cannot find error in simple WHILE statement

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 ;

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 5.6 Function error statement

I try to write mysql function. I have mysql 5.6, and I don`t know why every time i try do it:
CREATE FUNCTION do_it (s INT) RETURNS INT
BEGIN
DECLARE k INT;
SET k= s + 2;
RETURN k;
END;
I get:
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 1 */
I know it is basic mistake, but I don`t know how I can fix it...
You need to change the delimiter that MySQL is using or it will stop processing the CREATE request after the first semi-colon.
Do this:
DELIMITER $$
CREATE FUNCTION do_it (s INT) RETURNS INT
BEGIN
DECLARE k INT;
SET k= s + 2;
RETURN k;
END$$
DELIMITER ;

mysql : What is wrong with my stored procedure?

Its my first time to learn stored procedure. I have here below :
DELIMETER //
DROP PROCEDURE IF EXISTS RepeatTomProc //
CREATE PROCEDURE RepeatTomProc()
BEGIN
DECLARE x INTEGER;
SET x = 1;
REPEAT
INSERT INTO TOM VALUES(x);
SET x = x + 1;
UNTIL x > 100
END REPEAT;
END //
DELIMITER;
What I Want to happen is that when the loop is not yet done it will insert the x value into the table TOM.
As for now the table TOM is empty.
for the table :
CREATE TABLE TOM (
TOM_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
PRIMARY KEY (TOM_ID)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
My problem is when I try to create the stored procedure by copying and pasting the code in my mysql terminal I have the following error :
mysql> DELIMETER $$
-> DROP PROCEDURE IF EXISTS RepeatTomProc $$
-> CREATE PROCEDURE RepeatTomProc()
-> BEGIN
-> DECLARE x INT;
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 'DELIMETER $$
DROP PROCEDURE IF EXISTS RepeatTomProc $$
CREATE PROCEDURE RepeatTo' at line 1
mysql> SET x = 1;
ERROR 1193 (HY000): Unknown system variable 'x'
mysql> REPEAT
->
Display all 748 possibilities? (y or n)
? DESC ITERATE ORD
// It displays lots of other possibilities which I have ommitted
-> = x + 1;
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 'REPEAT
= x + 1' at line 1
mysql> UNTIL x > 100
-> END REPEAT;
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 'UNTIL x > 100
END REPEAT' at line 1
mysql> END $$
-> DELIMITER;
I have based my codes from : Loop in Stored Procedures
You mistyped DELIMITER
try this
DELIMITER //
DROP PROCEDURE IF EXISTS RepeatTomProc //
CREATE PROCEDURE RepeatTomProc()
BEGIN
DECLARE x INTEGER;
SET x = 1;
REPEAT
INSERT INTO TOM VALUES(x);
SET x = x + 1;
UNTIL x > 100
END REPEAT;
END //
DELIMITER ;
Also you need space between DELIMITER and ; in the end