Home All Groups Group Topic Archive Search About

Escape Code for Stored Proc

Author
25 Aug 2006 6:52 PM
MichaelT
Hi guys n gals, Im on hunt for an escape code or command for my TSQL
stored proc..

If Something = SomethingElse
     <Exit the Stored Proc>

I feel a little dumb asking this but I Could not find any thing
relating to

termination
aborting
Exiting
Ending
etc...

Regards
Michael

Author
25 Aug 2006 7:06 PM
Arnie Rowland
RETURN

--
Arnie Rowland, Ph.D.
Westwood Consulting, Inc

Most good judgment comes from experience.
Most experience comes from bad judgment.
- Anonymous


Show quote
"MichaelT" <Michael.Thomlin***@gmail.com> wrote in message
news:1156531921.262255.289100@i3g2000cwc.googlegroups.com...
> Hi guys n gals, Im on hunt for an escape code or command for my TSQL
> stored proc..
>
> If Something = SomethingElse
>     <Exit the Stored Proc>
>
> I feel a little dumb asking this but I Could not find any thing
> relating to
>
> termination
> aborting
> Exiting
> Ending
> etc...
>
> Regards
> Michael
>
Author
25 Aug 2006 7:07 PM
PCTC_IT
I believe RETURN is what your looking for:

Exits unconditionally from a query or procedure. RETURN is immediate and
complete and can be used at any point to exit from a procedure, batch, or
statement block. Statements that follow RETURN are not executed

Syntax

RETURN [ integer_expression ]


Arguments
integer_expression
Is the integer value that is returned. Stored procedures can return an
integer value to a calling procedure or an application.

Return Types
Optionally returns int.

Note: 
Unless documented otherwise, all system stored procedures return a value of
0. This indicates success and a nonzero value indicates failure.


Show quote
"MichaelT" wrote:

> Hi guys n gals, Im on hunt for an escape code or command for my TSQL
> stored proc..
>
> If Something = SomethingElse
>      <Exit the Stored Proc>
>
> I feel a little dumb asking this but I Could not find any thing
> relating to
>
> termination
> aborting
> Exiting
> Ending
> etc...
>
> Regards
> Michael
>
Author
25 Aug 2006 7:09 PM
Aaron Bertrand [SQL Server MVP]
RETURN;



Show quote
"MichaelT" <Michael.Thomlin***@gmail.com> wrote in message
news:1156531921.262255.289100@i3g2000cwc.googlegroups.com...
> Hi guys n gals, Im on hunt for an escape code or command for my TSQL
> stored proc..
>
> If Something = SomethingElse
>     <Exit the Stored Proc>
>
> I feel a little dumb asking this but I Could not find any thing
> relating to
>
> termination
> aborting
> Exiting
> Ending
> etc...
>
> Regards
> Michael
>
Author
4 Sep 2006 1:09 PM
craig_amtdatatechnologies@discussions.mi
Hi,

  Your question implied a how to code for abnormal termination of procedures
(?)

   RETURN will force exit from the procedure, but this may not be suitable
for all  situations e.g. SQL 2000 error-handling

  e.g. IF @@ERROR <> 0 , maybe running some audit trail code might be useful

One construction thate can be used is:-


..... <body of procedure>

    SELECT @intError = @@ERROR
    IF @intError <> 0
        GOTO procedureError

...... <more of the procedure>

   -- Procedure success
  GOTO endProcedure                     -- Must have this otherwise code in
'procedure error' is run

produreError:-

  --  do stuff in respone to error

endProcedure:-

  -- do other stuff regardless of what happens

RETURN


SQL 2005 of course replaces the above with BEGIN TRY ... END TRY BEGIN CATCH
.... END CATCH


The above constructions will ensure a single point of exit from procedures,
and allow you to consistently handle how a procedure exits, and sets any
relevant values (e.g. record failure or success in an audit trail table or
whatever ..)

Hope the above helps.


Craig










Show quote
"MichaelT" wrote:

> Hi guys n gals, Im on hunt for an escape code or command for my TSQL
> stored proc..
>
> If Something = SomethingElse
>      <Exit the Stored Proc>
>
> I feel a little dumb asking this but I Could not find any thing
> relating to
>
> termination
> aborting
> Exiting
> Ending
> etc...
>
> Regards
> Michael
>
>

AddThis Social Bookmark Button