Problem: How to control the
flow of the execution and jump to a block code.
Alters the flow of execution to a label.
The Transact-SQL statement or statements that follow GOTO are skipped and
processing continues at the label. GOTO statements and labels can be used
anywhere within a procedure, batch, or statement block. GOTO statements can be
nested.
Example:
In this
article I am going to show you how GOTO can be used to control the flow of
execution.
Please see
the script below:
CREATE PROCEDURE dbo.InputValidation
@inputvalue
int
AS
BEGIN
IF
@inputvalue %2=0
GOTO Branch_Even --Jumps
to the first branch.
IF
@inputvalue %2<>0
GOTO Branch_Odd
--This will never execute.
Branch_Even:
SELECT 'Jumping To Branch Even.Input value is Divisible by 2 and is
a even number'
RETURN
Branch_Odd:
SELECT 'Jumping To Branch Odd. Input Value is an Odd Number';
END
Time to
Test, the work.
EXEC dbo.InputValidation 15
EXEC dbo.InputValidation 12
OUTPUT:
0 comments:
Post a Comment