Question: How to Use GOTO command in SQL Server?
Answer: While I am not a great fan of this command let us see a simple example of how GOTO works in SQL Server. Basically, this statement changes the flow of the execution. While running the SQL code wherever the execution encounters the GOTO statement, then it will jump unconditionally to the label specified in the goto statement. Let us see a simple example.
First, create the following stored procedure:
CREATE PROCEDURE MySP (@Param INT) BEGIN IF @Param > 100 GOTO OVER100 IF @Param > 10 GOTO OVER10 IF @Param = 0 GOTO ZERO SELECT 'Value is single Digit' Result RETURN OVER100: -- Complex Process SELECT 'Values is Over 100' Result RETURN OVER10: -- Complex Process SELECT 'Values is Between 10 and 100' Result RETURN ZERO: