The GOTO statement in T-SQL allows you to transfer control to a labeled line of code within a stored procedure or a batch. The labeled line is identified by a unique label name followed by a colon (:). Here is an example of how you can use the GOTO statement in T-SQL:
DECLARE @Value INT = 1
IF @Value = 1
BEGIN
PRINT ‘Value is 1’
GOTO EndOfCode
END
PRINT ‘Value is not 1’
EndOfCode:
PRINT ‘End of code’
In this example, the IF statement checks if the value of the @Value variable is equal to 1. If the value is 1, the PRINT statement outputs “Value is 1” and control is transferred to the labeled line of code identified by EndOfCode using the GOTO statement. The execution continues from that line and the PRINT statement outputs “End of code”.
It is important to note that the use of the GOTO statement is generally discouraged in T-SQL programming as it can make the code difficult to understand and maintain. It is recommended to use structured control flow statements such as IF, WHILE, and FOR instead of GOTO.