添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

How to Use GOTO command in SQL Server? – Interview Question of the Week #249

Question: How to Use GOTO command in SQL Server?

How to Use GOTO command in SQL Server? - Interview Question of the Week #249 gotocommand-800x285

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: