GOTO command in SQL Server
The GOTO command is a simple jump command, which allows an unconditional jump program from GOTO to a location in the program that has a label (laber) command in the same function. This article will detail how to use this command in SQL Server.
Syntax
The GOTO statement in SQL Server consists of two parts: Commands and labels. We use the following syntax:
The GOTO command includes the GOTO keyword enclosed with the label name label_name
GOTO label_name;
The label section includes the label name label_name and the statement to execute next.
label_name: {. next execution statement .}
Note:
- label_name must be unique within the function scope.
- There must be at least one command to execute after declaring the label.
For example
DECLARE @Number INT = 1 ;
DECLARE @Total INT = 0 ;
WHILE @Number < = 10
BEGIN
IF @NUMBER = 5
GOTO quantrimang;
ELSE
SET @Total = @Total + @Number;
SET @Number = @Number + 1 ;
END;
quantrimang
PRINT @Total;
GO
In the example using this GOTO statement, we created a label named quantrimang, when the @NUMBER variable reached value 5, the program allowed to jump to the position of the quantrimang label in the function and execute the statement following it. .
See more:
- IF commands . ELSE in SQL Server.
- WHILE loop in SQL Server.
- BREAK (Control Interrupt) command in SQL Server.
Previous lesson: CONTINUE command in SQL Server
You should read it
May be interested
- Instructions for installing SQL Server 2019on september 24, microsoft announced the release of sql server 2019 community technical preview (ctp) 2.0. it is very suitable for database professionals to keep up with modern technology.
- WHILE loop in SQL Serverthe while loop is used if you want to run a code repeatedly when the given condition returns true. let's find out how to use while in sql server with network administrator.
- FOR loop in SQL Serverthe for loop is often used to run a code repeatedly for the number of repetitions. however, in sql server there is no for loop.
- ASCII function in SQL Serverthe article will explore and guide you how to use the ascii () function in sql server.
- CHAR function in SQL Serverchar function in sql server is used to convert an integer expression into the corresponding character in ascii code.
- CHARINDEX function in SQL Serverthe charindex function in sql server is used to search for a substring within a large string starting from the specified position.