3

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.

3
  • Is this code wrapped in a function or stored procedure? The WHILE needs to be executed in that context. Commented Feb 22, 2013 at 16:44
  • No i'm just writing it in the query area where the queries are written Commented Feb 22, 2013 at 16:46
  • If I simply write CALL mypro() it works fine Commented Feb 22, 2013 at 16:47

1 Answer 1

7

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thankyou but I dont have enough reputation points to do that whereas I have already clicked on the tick

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.