mysql : What is wrong with my stored procedure? - mysql

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

Related

How to create a procedure to generate a range of numbers in MySQL?

I'm trying to create a procedure to generate a range of numbers between 2 integers L and H. Here is my code:
CREATE TABLE Numbers (n INT);
DELIMITER //
CREATE PROCEDURE GenerateNumbers(IN L INT, IN H INT)
BEGIN
DECLARE I INT UNSIGNED DEFAULT L;
WHILE I <= H DO
INSERT INTO Numbers(n) VALUES(I);
SET I = I + 1;
END WHILE;
END //
DELIMITER;
Whenever I run the code it returns this error
ERROR 1064 (42000) at line 16: 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 'DELIMITER' at line 1
Can anyone help?
So this is a simple one and you're going to kick yourself - you just need a space between DELIMITER and the semi-colon:
DELIMITER ;
Instead of:
DELIMITER;

What is the matter with if statement in procedure?

It is simple to write a sql procedure.
demiliter //
create procedure show_growth()
begin
SELECT * from tb;
end //
I want to add a if statement in the procedure.
Drop it first.
drop procedure show_growth //
Then create a new one.
create procedure show_growth(in type char(3))
-> begin
-> if type = "all" then
-> SELECT * from tb;
-> endif
-> end //
ERROR 1064 (42000): 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 'end' at line 6
How to fix it?
endif is not 1 word.
begin
if type = "all" then
SELECT * from tb;
end if;
end

MySql Trigger Syntax Error 1064 Empty Single Quotes

I am trying to execute trigger statement in MySql 5.1.41 but i am getting an error which is beyond my comprehension. Any help will be appreciated.
Following is the trigger code:
create TRIGGER populate_modality_trigger AFTER INSERT on series
FOR EACH ROW BEGIN
DECLARE x int;
select count(*) into x from lu_modality_alias l WHERE l.modality=NEW.modality;
IF (x =0 OR x is NULL) THEN
INSERT INTO lu_modality_alias
set modality = NEW.modality,
modality_alias = NEW.modality
;
END if;
end;
When I run above code, I get following 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 3
The interesting thing is that when i execute the same code from Naviacat query editor, it runs successfully and trigger is created.
But the same fails from phpMyAdmin sql.
You need to change the delimiter character so the MySql parser doesn't end the CREATE statement parsing at the first semi-colon
delimiter //
create TRIGGER populate_modality_trigger AFTER INSERT on series
FOR EACH ROW BEGIN
DECLARE x int;
select count(*) into x from lu_modality_alias l WHERE l.modality=NEW.modality;
IF (x =0 OR x is NULL) THEN
INSERT INTO lu_modality_alias
set modality = NEW.modality,
modality_alias = NEW.modality
;
END if;
end //
delimiter ;

MySQL Nested Blocks Error 1064

Can anyone see why I would get:
[Err] 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 '//
DELIMITER' at line 11
For the following snippet:
DELIMITER //
DROP PROCEDURE
IF EXISTS nested_test//
CREATE PROCEDURE nested_test()
BEGIN
DECLARE a INT;
SET a = 1;
SELECT a;
BEGIN
DECLARE b INT;
SET b = 2;
SELECT b;
END;
END//
DELIMITER;
This is a simplified version of what I am actually writing and it brings up exactly the same error. Everything is great until I add the nested BEGIN END block. It works perfectly well on phpMyAdmin but fails on Navicat 9
It should have a space after DELIMITER.
DELIMITER ;
Like that.
DELIMITER //
DROP PROCEDURE
IF EXISTS nested_test//
CREATE PROCEDURE nested_test()
BEGIN
DECLARE a INT;
SET a = 1;
SELECT a;
BEGIN
DECLARE b INT;
SET b = 2;
SELECT b;
END;
END //
DELIMITER ;
On execution, it generates:
0 rows affected, 0 rows found. Duration for 2 queries: 0.000 sec.

Can any one suggest me how to resolve this problem: Error Code : 1064 in MY SQL 5.5 ver

DELIMITER $$;
DROP FUNCTION IF EXISTS tonumeric $$;
CREATE FUNCTION tonumeric() returns numeric
BEGIN
declare num numeric;
set num = to_number('12');
return num;
END$$
DELIMITER; $$
When I executed this function, I am facing this error.
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 'IF EXISTS tonumeric' at line 1
(0 ms taken)
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 ';
CREATE FUNCTION tonumeric() returns numeric
BEGIN
declare num numeric;
set num' at line 1
(0 ms taken)
Thanks
How about this:
DELIMITER $$
DROP FUNCTION IF EXISTS tonumeric $$
CREATE FUNCTION tonumeric() returns numeric
BEGIN
declare num numeric;
set num = to_number('12');
return num;
END$$
DELIMITER ;
Delimiter is a special command, in that you shouldn't terminate it with a ; -- you're actually setting the delimiter to "$$;", not "$$".