473,549 Members | 2,670 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 33776

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
Thomas Johnson
1 New Member
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
6909
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
15576
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 this procedure it reads a dataset and return the first line of the dataset. I met two problems: Problem 1:
4
45073
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: Function ThisFunctionInsertsANewRecord() On Error GoTo myErr Dim cmd As ADODB.Command
0
19261
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 hell of a time with it... The following comes from phpinfo(): PHP Version: 5.1.2 mysql Client API version: 5.0.18 mysqli Client API version: 5.0.18
7
9686
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 for this being to stop the user thinking the application has frozen when in fact it is just waiting for a long SP to complete. Another reason for...
0
3481
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 ORACLE_HOME=$ORACLE_BASE/product/9.2.6 export PATH=$ORACLE_HOME/bin:/usr/ccs/bin:/bin:/usr/bin:/usr/local/bin:/usr/ucb
3
6188
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 NEW AS POST FOR EACH ROW MODE DB2SQL BEGIN ATOMIC CALL GGWU.PKG_MULT_PROP_INSERT(POST.C_PROPERTY_CODE,POST.I_MULTIPLIER_ID); END@
2
3398
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 well as a value for the exit status of the SP. If the record doesn't exist, it selects an error message as the exit status. I've recreated the SP...
2
5625
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 the tables and their links. When getting the data from the DB... well suffice to say it's ugly. I want to use one stored procedure that will return a...
0
7521
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7451
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7720
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7473
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7810
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5369
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5088
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3501
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1061
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.