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 362 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'


If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.

Leave a Comment


You might be Interested in these Posts

Deleting all the hidden rows and columns in the active worksheet using VBA

Do you want to delete all the hidden rows and columns in the active worksheet in Microsoft Excel usi...

Displaying all the worksheet names in Excel using VBA Code

Do you want to display the names of the worksheets in the excel workbook ?. Here;s how you can do it...

How to change positive numbers to negative in Microsoft Excel 2016 ?

Do you want to change the positive numbers to negative numbers and vice versa (or in simple terms) ,...