After
a condition/statement execution, if we need to alter the flow of execution to
start from a particular label, then the GOTO statement will help you. Basically,
GOTO keyword uses to skip statement process and continue from a particular
label.
GOTO
Statement in SQL Server contains two parts –
GOTO
statement declaration: GOTO statement contains GOTO keywords and label_name as
below.
GOTO label_name
|
Label
Declaration: The label contains label_name only with at least one statement as below.
label_name:
select * from tbl
|
Label_name
must be unique within the scope of the code and after label_name declaration,
there should be one SQL-statement.
Example:
How can we use GOTO statement in SQL Server, you can see below.
DECLARE @rating INT=10
WHILE @rating>0
BEGIN
IF @rating=8
GOTO Codefari
SET @rating=@rating-1
PRINT @rating
END
Codefari:
PRINT 'www.codefari.com'
|
In
above example, we have created one GOTO statement lable_name is Codefari if the
@rating value is 8 then code will branch to the label called Codefari.