How to call a stored procedure using while loop in mysql - mysql

My question may sound lame but still I cant get whats wrong with this code. I have made a stored procedure in sqlyog and now I want to call it. I have tried almost everything but it doesn't work.
SET #x=1;
WHILE #x<=10 DO
CALL mypro();
SET #x=#x+1;
END WHILE;
Thanks in advance.

Flow control statements like IF, WHILE need to be executed in context of a function or stored procedure. If you wish to execute mypro() in a loop, that action itself must be created as a procedure. Below I will create a procedure called call_mypro_x10() which calls your original stored procedure in a WHILE loop.
DELIMITER $$
CREATE PROCEDURE call_mypro_x10()
BEGIN
SET #x = 1;
WHILE #x <= 10 DO
CALL mypro();
SET #x := #x + 1;
END WHILE;
END$$
DELIMITER ;
Then call this procedure which wraps the other one:
CALL call_mypro_x10();
Note that the DELIMITER statements may not be necessary in all clients, and some MySQL clients supply a different method of specifying an alternate delimiter needed in stored procedure and function definitions. Apparently SQLyog supports DELIMITER
There is an open feature request to permit flow control statements as part of normal query execution.

Related

MySQL Cursors - having errors in MySQL Workbench

I am trying to write a mysql program, which has cursors in it. Due to an error in writing an DECLARE query, MySQL Workbench is always showing me the DECLARE is not valid at this position, expected EOF, ALTER, ANALYZE, BEGIN, BINLOG, CACHE, ...
Could you help me solve this problem?
Here is my code:
DELIMITER //
BEGIN
declare Naslov_knjige VARCHAR(24);
declare Cena_knjige DECIMAL(8,2);
DECLARE cursor_cene CURSOR
FOR SELECT
Naslov,
Cena
FROM
prvi_test_v2.knjige;
OPEN cursor_cene //
FETCH NEXT FROM cursor_cene INTO
#Naslov_knjige,
#Cena_knjige //
WHILE ##FETCH_STATUS = 0
BEGIN
PRINT #Naslov_knjige + CAST(#Cena_knjige AS VARCHAR) //
FETCH NEXT FROM cursor_cene INTO
#Naslov_knjige,
#Cena_knjige //
END //
CLOSE cursor_cene //
DEALLOCATE cursor_cene //
END //
DELIMITER ;
Thanks for your help!
I will assume you omitted a line for CREATE PROCEDURE, because in MySQL a BEGIN...END block must be part of a stored routine. See https://dev.mysql.com/doc/refman/8.0/en/begin-end.html
BEGIN ... END syntax is used for writing compound statements, which can appear within stored programs (stored procedures and functions, triggers, and events).
You changed the DELIMITER:
DELIMITER //
Using this delimiter terminates the whole CREATE PROCEDURE statement. You should not do this after the first statement in the body of the procedure. You need to use the normal ; terminators for each statement within the body of the procedure. The reason for changing the delimiter is so you can use ; for each statement in the procedure without ending the CREATE PROCEDURE.
See examples and documentation here: https://dev.mysql.com/doc/refman/8.0/en/stored-programs-defining.html
That's the reason for the error you got. You used // to terminate OPEN cursor_cene // which ended the CREATE PROCEDURE, but clearly there was more to that procedure.
There are other problems with your procedure. You seem to be using Microsoft SQL Server syntax, but MySQL is different.
Naslov_knjige is not the same variable as #Naslov_knjige in MySQL. Don't use the # sigil in front of local variables. If you use the # sigil, this refers to a user-defined variable.
The WHILE ##FETCH_STATUS = 0 syntax is specific to Microsoft SQL Server. MySQL has different syntax for running a cursor loop. See example in the documentation: https://dev.mysql.com/doc/refman/8.0/en/cursors.html
That's as far as I got. There may be more problems, but I am not going to look for them.

Why delimiter used with stored procedure in mysql?

I am trying to understand, why delimiter used with stored procedure in mysql?
but i couldn't.
DELIMITER //
CREATE PROCEDURE GetAllProducts()
BEGIN
SELECT * FROM products;
END //
DELIMITER ;`
Mysql's default delimiter is ; which is used for one statement in the command line , something as
select * from users ;
When you write a trigger or stored procedure to execute the entire code mysql needs to understand that its a block of code/query.
If no delimiter is provided then when mysql encounters any ; inside the store procedure or trigger it will think that as one statement and will try to execute it. So we need to provide a delimiter for store procedure or trigger and make mysql understand that anything within that delimiter is one complete set of code.
So in your example
SELECT * FROM products;
it will be a part of the complete statement when there is a delimiter other than ; is provided at the beginning.

Returning a value from a MySQL Stored Procedure when completed

Currently I have a stored procedure which works fine without the out variable, but i wanted to put one in so that i can ensure that my code on the Delphi Application does no run until the Stored Procedure has finished.
Can someone see if I have done this right as i keep getting an error regarding out not being a variable. Is anyone able to explain this?
Please see below my code I am using.
DELIMITER $$
CREATE DEFINER=`root`#`localhost` PROCEDURE `proc_moveToWizconTemp`(
OUT success int)
BEGIN
//Code to do some processing and end of SP Processing
SET success = 1;
END
Regards,
Jordan

MySQL Procedure Not Working

I'm working with procedures for the first time in MySQL, but for some reason I keep getting NULL. My test procedure is a simple one, it just adds.
delimiter $$
create procedure adds(in r double, out a double)
begin
set a = r + r;
end $$
delimiter ;
CALL adds(5, #a);
SELECT #a;
Not sure if I'm doing this right. For #a it just prints out NULL.
A procedure is linked to a database.
You have not specified one, and therefor it will probably be attached to a different database than the one you are expecting.
When you change databases, MySQL will not longer find your stored procedure because it only looks in the correct DB.
Remember to always specify your database when declaring a stored proc
create procedure mydatabase.adds(in r double, out a double)
^^^^^^^^^^^

Suppose I want to create a "stored function" for MYSQL. Where do I wrote it? Which file?

Of course, I could go into mysql console and type the Function. But what if I want to store it for future use? What do I do?
Most projects have an SQL file to initialize the database from scratch. This way, one can set up the application database by simply running this SQL file. Your CREATE PROCEDURE/FUNCTION query would also go in this file.
There's a good tutorial here:
http://www.databasejournal.com/features/mysql/article.php/3525581/MySQL-Stored-Procedures-Part-1.htm
You need to use stored procedures. Once written, these are stored in the database along with your tables. They can be invoked using the CALL <procedure> statement.
Here's an example procedure that populates table1 with random values:
DELIMITER //
DROP PROCEDURE IF EXISTS autofill//
CREATE PROCEDURE autofill()
BEGIN
DECLARE i INT DEFAULT 0;
WHILE i < 20000 DO
INSERT INTO table1 (size) VALUES (FLOOR((RAND() * 1000)));
SET i = i + 1;
END WHILE;
END;
//
DELIMITER ;
Once the procedure has been written, it is called like this:
CALL autofill();