GOTO Label in SQL Server

Published on August 25, 2016 by abundantcode

Filed under SQL Server

Last modified August 25, 2016

Print this page

rate 1 star rate 2 star rate 3 star rate 4 star rate 5 star
Your rating: none, Average: 5 (1 votes)

This article have been viewed 4086 times

There are times when you want to jump to a specific section (Label) in the code when a condition satisfies. You can create a label and then use the GOTO statement to branch directly to the code.

To create a label , simply specify the label name followed by the colon.

Label1:

Here’s a query demonstrating the usage of the Label named "RecordExistsLabel" and GOTO statement.

use AdventureWorks2014
GO
IF EXISTS (SELECT BusinessEntityID     
                FROM HumanResources.Employee   
                 WHERE MaritalStatus = 'S')  
BEGIN     
    GOTO RecordExistsLabel
END;  
PRINT 'Record does not exist'
RecordExistsLabel:
SELECT *  FROM HumanResources.Employee   WHERE  MaritalStatus = 'S';  
PRINT 'Record Exists'

image

Leave a Comment


x

You might be Interested in these Posts

C Program to find the G.C.D of a number using Recursive function

Problem Write a program in C to find the find the G.C.D of a number using recursion and display the ...

C Program to print a Half Pyramid using *

Problem Write a program in C to print Half pyramid using * as shown. * * * * * * * * * * ...

C Program to display Positive factors of a number

Problem Write a program in C to display all the positive factors of a number enter by the user. Ho...