473,382 Members | 1,809 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,382 software developers and data experts.

how to exit from stored procedure?

-
in my stored procedure i have some if else statements:
SET @status = "";

IF (code is invalid...
SET @status = "Invalid code.";
SELECT @status;
END IF;

IF (name is invalid..
SET @status = "Invalid name.";
SELECT @status
END IF;

IF (code and name already exists)
SET @status = "Record exists.";
SELECT @status
ELSE
SET @status = "Record added.";
SELECT @status
END IF;

SELECT @status;

The @status always return the value from the last IF .. ELSE statement.
Since RETURN can't be used in a procedure, what syntax should i use to
immediately exit the stored procedure if the first IF statement is true?
Jul 23 '05 #1
4 33759

Just a guess here, but you are issuing SELECT @STATUS more than once.
That probably creates more than one returned recordset, and your client
is only seeing the first one.

---
Steve

Jul 23 '05 #2
-
Steve wrote:
Just a guess here, but you are issuing SELECT @STATUS more than once.
That probably creates more than one returned recordset, and your client
is only seeing the first one.

---
Steve


it's actually returning only one @status depending on the condition.

The SELECT @status in each if else statement is where the syntax is
wrong. Is there a way to exit the procedure immediately after each
SELECT? I noticed that other databases uses RETURN but mysql does not
allow RETURN in procedures.

Jul 23 '05 #3

There may not be any such language construct to do what you want to do,
so you will probably have to rearrange your logic.

I can only restate that your procedure ALWAYS issues at least two
SELECT @STATUS statements, each of which creates a resultset.

The first occurs in this code:

IF (code and name already exists)
SET @status = "Record exists.";
SELECT @status
ELSE
SET @status = "Record added.";
SELECT @status
END IF;

where one or other branch must be executed, and both have a SELECT
@STATUS, so one of those SELECT @STATUS statements will be executed.
The second occurs in this code:

SELECT @status;

which is an unconditional statement and will always be executed. Hence
a minimum of two SELECT @STATUS statements will be executed.

My advice would be to recode the conditional blocks to only SET the
value of @STATUS, not SELECT it. SELECT it unconditionally only once at
the end of the procedure. That way, you will definitely only get one
resultset back from the procedure call.

You might also experiment with a cheat using LOOP/END LOOP/LEAVE:

LOOP

IF cond
statements
LEAVE
END IF

IF cond
statements
LEAVE
END IF

LEAVE

END LOOP

---
Steve

Jul 23 '05 #4
I had teh same issue last night. I found the easiset way is to add another Begin and End just inside the procedure using a label.

Create Procdure MyProc()
Begin
main: begin
{Procedure Logic Here....}
End main
End

Then anytime you want to exit use: leave main
Jul 13 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Lauren Quantrell | last post by:
Just wondering if this is good form: Alter Procedure "mySPName" @UniqueID int AS set nocount on set xact_abort off DELETE FROM tblNameOne
7
by: Jeff Wang | last post by:
Hi all, Can someone help me out? I've been struggling with this for almost a week and still have no clue what's wrong. Basically I want to write a DB2 stored procedure for OS/390 in REXX. In...
4
by: laurenq uantrell | last post by:
I need to get the value of an output parameter back into my VBA function calling a stored procedure. I'm using the following construction to append a new record in a SQL Server table: ...
0
by: IamtheEvster | last post by:
Hi All, I am currently using PHP 5 and MySQL 5, both on Fedora Core 5. I am unable to call a MySQL stored procedure that returns output parameters using mysql, mysqli, or PDO. I'm having a...
7
by: Siv | last post by:
Hi, I have a stored procedure that I want to execute and then wait in a loop showing a timer whilst it completes and then carry on once I get notification that it has completed. The main reason...
0
by: istruttorenuoto | last post by:
hi! I have some problems to call a stored procedure from an Unix Script. Here's the script stty istrip stty erase ^H export ORACLE_BASE=/product/oracle export...
3
by: yinzara | last post by:
I have the following trigger that calls a DB2 stored procedure: DROP TRIGGER GGWU.TRI_A_MULTI_PROP@ CREATE TRIGGER GGWU.TRI_A_MULTI_PROP AFTER INSERT ON GGWU.MULTIPLIER_PROPERTY REFERENCING ...
2
by: kxyz | last post by:
Hello everyone, I need help with a stored procedure or two. My stored procedures are supposed to check if a certain record exists. If it does exist, then I select everything from that row, as...
2
by: IuliaS | last post by:
Hello everyone! I want to create a stored procedure, so I can more easily, and transparent retrieve data from db2. Long story short: when a user wants to put some data in the DB, he also creates...
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: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...

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.