473,379 Members | 1,530 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

How to use the RETURN keyword in a MySQL Stored Procedure?

Hi all,
as part of my project requirement we are migrating procedures from mssql to mysql
below is the code in mssql
Expand|Select|Wrap|Line Numbers
  1. DECLARE 
  2.      "@rowcnt" int
  3.     select"@rowcnt" = count(*) from tg_services WHERE name ="@name"
  4.     IF (@rowcnt <> 0)
  5.     BEGIN
  6.       -- print"@name" + ' already exists in the tg_services table'
  7.       return
  8.     end
  9.     else
  10.     BEGIN
  11.       select"@rowcnt" = count(*) from tg_services WHERE service_id ="@serviceid"
  12.       IF (@rowcnt <> 0)
  13.       BEGIN
  14.         --print 'serviceid '+ convert(varchar(10),@serviceid) + ' already exists in the tg_services table'
  15.         return
  16.       end
  17.       else
  18.       BEGIN
  19.         INSERT into tg_services VALUES(@serviceid,@name)
  20.       end
  21.     end
  22.   end
  23.  
my question is:
in the above code they are returning from the procedure if rowcount <>0
else
doing some other bussiness
how do i get the same behaviour in mysql?
please do the need full.

Thanks in Advance
Vijaya
Aug 12 '08 #1
1 28197
Atli
5,058 Expert 4TB
Hi.

The RETURN keyword is not used in MySQL procedures.

If you need a stored procedure to return a value, you should use a FUNCTION.

But, if you need to exit a stored procedure, as done in your MSSQL example, you can use the LEAVE keyword.
That this would require you to "label" your BEGIN ... END block.

For example:
Expand|Select|Wrap|Line Numbers
  1. CREATE PROCEDURE SelectTest()
  2. root:BEGIN
  3.     SELECT 'First';
  4.     LEAVE root;
  5.     SELECT 'Second';
  6. END;
  7.  
There, the second SELECT will never be executed.

Also, just to put it out there.
Your problem could also be solved by simply altering the flow of your code.

Rather than checking if the value IS something, and calling the RETURN keyword, only performing some action if the ELSE clause is reached...
Try checking if the value IS NOT something, and simply perform the action then, letting the function end if the value IS something.

For example:
Expand|Select|Wrap|Line Numbers
  1. /* Rather than doing */
  2. root:BEGIN
  3.     DECLARE s INT DEFAULT 0;
  4.     SELECT COUNT(*) INTO s FROM someTable;
  5.  
  6.     IF s =  0 THEN
  7.         LEAVE root;
  8.     ELSE
  9.         /* Perform some action */
  10.     END IF;
  11. END;
  12.  
  13. /* Do */
  14. BEGIN
  15.     DECLARE s INT DEFAULT 0;
  16.     SELECT COUNT(*) INTO s FROM someTable;
  17.  
  18.     IF s != 0 THEN
  19.         /* Perform some action */
  20.     END IF;
  21. END;
  22.  
Aug 16 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Daniel | last post by:
hi ng, i am newbie to sqlserver and my problem seems simple, but i didn't find information about it: How can i display the RETURN @x value of a stored procedure in the sql analyzer of the...
0
by: robindangwal | last post by:
Hi, I've this problem where I have to invoke a MySql stored procedure from my C program. I searched a lot for this. But I couldn't find any solution to this problem. Please help me how to do...
0
by: IGKogan | last post by:
3 mounth ago I serfed web to find some kind of mysql stored procedure debugger. But I found nothing. Last week at last a tool with integrated debugger was released. MySql Developer Studio 2.0...
1
by: Muhammad Intikhab Qaiser | last post by:
Hi I am developing a parser to parse the stored procedure and function definition of MySQL.I need to extract the names of stored procedures,functions and tables which are being used in any...
3
by: Penstar | last post by:
I'm sorry if this is a really stupid question, but I have just started using mySQL. I have created a basic stored proc, which does the following: SELECT * from members; I can call it from...
1
by: sbettadpur | last post by:
hello i want to call mysql stored procedure from php, if anybody knows please explain me clearly or any url regarding this subject
2
by: zombiemasta | last post by:
Hi, I've wrote a stored procedure which expects a parameter to be passed in on the WHERE clause for channelID. The stored procedure runs fine in Navicat for MySQL when you specify the parameter...
12
by: Dooza | last post by:
I have a stored procedure that takes a number of inputs, does a bulk insert, and then outputs a recordset. When I run the stored procedure in Server Management Studio I also get a return value from...
5
by: Urmi Biswas | last post by:
Hi All, Has anyone tried calling MySQL stored procedure from php scripts? I use PEAR DB module to access MySQL database. Not sure if php 4.4.0 (that we use) support calling stored procedures. ...
0
by: SLauren | last post by:
Hi, Can anyone please help me how can i debug a mysql stored procedure using toad software?? Thanks in advance, Lauren
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.