GOTO Label in SQL Server

Published on August 25, 2016 by Senthil Kumar

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 7729 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.


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

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


Leave a Comment


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...