We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

MySQL Equivalent of 'return' in a stored procedure

mijk
mijk asked
on
Medium Priority
789 Views
Last Modified: 2013-12-07
Hi, I am in the process of converting an MSSQL DB over to MySQL and have run into a problem with my stored procedures.

I often put logic into my stored procedures for data validation etc and if something passed in is not valid then I stop the execution of the stored procedure by using the 'return' command in MSSQL

i.e.

-- validate input
if (@input = 'bad')
begin
  return
end

-- do something else
insert into some_table values (...)

However I cannot find an equivalent way of stopping the execution of the stored proc in MySQL as 'return' is not allowed.  Some of these procs get quite long so I dont want to add a load of nested if statements.

Does anyone know a way of doing this in MySQL?

Thanks


Comment
Watch Question

Guy Hengel [angelIII / a3]Billing Engineer
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2009

Commented:
yes, mysql has the return statement:
http://dev.mysql.com/doc/refman/5.1/en/return.html

Commented:
How about

-- validate input
if (@input <> 'bad')
begin
  -- do something else
  insert into some_table values (...)
end


Guy Hengel [angelIII / a3]Billing Engineer
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2009

Commented:
I see the error: you need to write like this:
if (_input = 'bad') then
  return;
end if;

Open in new window

Author

Commented:
MySQL says that it is an incomplete statement and wont accept the stored proc going into MySQL.

See example below



create procedure do_something(_input varchar (64))
begin
 
  if (_input = 'bad') then
    return;
  end if;
 
  -- do something else....
 
end;

Open in new window

Billing Engineer
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2009
Commented:
I reread the docs:
http://dev.mysql.com/doc/refman/5.1/en/return.html

<...>The RETURN statement terminates execution of a stored function <...>

so indeed, they mean only stored function, and not stored procedure ... :(

in which case, you have to code like this:
create procedure do_something(_input varchar (64))
begin
 
  if (_input <> 'bad') then
 
  -- do something else....
  end if;
 
end;

Open in new window

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
Thanks for the feedback guys,

I have just found this on the MySQL forum.

Using the 'leave' keyword and a label type thingy.
create procedure do_something(_input varchar (64))
begin
 
mainproc:
  begin
 
    if (_input = 'bad') then
      leave mainproc;
    end if;
 
  -- do something else....
 
  end;
 
end;

Open in new window

Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.