Make homepage|Add to favorites
 

MYSQL TOOLS
reviews, articles, tips & snippets

 
Buy SQL » MySQL » How to exit from stored procedure.

How to exit from stored procedure.

Author: rootsql Date: 25-10-2012, 22:06

 The solution is very simple. We can use LEAVE statement to quit from any labeled flow control construct (LOOP, WHILE, REPEAT) -

CREATE PROCEDURE Foo()
BEGIN
  DECLARE i INT DEFAULT 0;
label_a:
  LOOP
    INSERT INTO table1 VALUES (i);
    SET i = i + 1;
    IF i > 9 THEN
      LEAVE label_a;
    END IF;
  END LOOP label_a;
END

 

In our case it is enough to place named label before the BEGIN...END clause and call LEAVE statement -

CREATE PROCEDURE Foo(IN param INT)
label_a:BEGIN
  -- Some code...
  IF param = 0 THEN
    LEAVE label_a; -- Exit from the procedure.
  END IF;
  -- Some code...
END
Dear visitor, you went to the site as unregistered user.
We recommend you to Register or Sign In.

Comments:

Leave a comment
Information
Members of Guests cannot leave comments.