GOTO statement is used to break execution of flow and move execution directly to label specified. The GOTO statement modifies the course of performance to a tag. The T-SQL statement or messages that pursue GOTO are ignored and execution resumes at the tag or label. GOTO statements and labels can be utilized anywhere within programs such as procedures, batch, or statement blocks.
GOTO can work within dependent control-of-flow information, statement obstructions, or methods, but it cannot move to a label beyond the batch. GOTO branching can move to a label specified before or after GOTO. The GOTO statement is a short and fundamental flow of control statement that generates an absolute modification in the course of control.
Implementation of the GOTO statement is normally believed to be a flawed programming approach and is not advised. Comprehensive usage of GOTO grooms to direct to substandard code particularly when procedures develop extended. Again, GOTO is not essential because there are more useful statements known to manage the performance path. There are no typical problems that demand the usage of GOTO; rather, it is more frequently employed for comfort.
If the GOTO statement is expressed in a combined statement, the label must be specified in the same block, unless it is in a hierarchical structure. If it is specified in a handler, the label must be described in the exact handler, accepting the different scope limitations.
Its syntax is as below:
Here, label names must be unique.
Example:
Most programmers avoid using GOTO. The main reason behind this is, it’ll make code look like spaghetti. It’ll make it difficult to understand the flow of control of your program. GOTO statement can easily replace using control-flow statements like IF-ELSE or WHILE.
There is a famous article by Edsger W. Dijkstra on the harmfulness of GOTO, take a look.