BREAK (Control Interrupt) command in SQL Server

The Break command used to exit the loop does not specify a stop condition or you want to stop the loop on condition that you specify and execute the statements following the END command of the loop.

Syntax

To use the command to interrupt BREAK control in SQL Server, we use the following syntax:

 BREAK; 

There are no parameters and arguments in the BREAK statement.

Note:

  1. You use BREAK to end the WHILE loop early.
  2. If in the code there is WHILE nested LOOP, BREAK will terminate the nearest WHILE loop.

For example

 DECLARE @Number INT = 1; 
DECLARE @Total INT = 0;

@Number WHILE <= 10
BEGIN
IF @NUMBER = 5
BREAK;
ELSE
SET @Total = @Total + @Number;
SET @Number = @Number + 1;
END

PRINT @Total;
GO

In this example using the BREAK statement, the WHILE loop will end when @Number reaches value 5.

Previous article: FOR loop in SQL Server

Next lesson: CONTINUE command in SQL Server

3.8 ★ | 5 Vote

May be interested

  • The sfc command in WindowsThe sfc command in Windows
    (applies to windows server (semi-annual channel), windows server 2016, windows server 2012 r2, windows server 2012)
  • CASE statement in SQL ServerCASE statement in SQL Server
    this article will show you in detail how to use the case statement handling function in sql server with specific syntax and examples to better visualize and capture functions.
  • SELECT TOP command in SQL ServerSELECT TOP command in SQL Server
    in sql server, the select top command is used to retrieve records from one or more tables in sql server and limit the number of return records based on a fixed value or percentage.
  • SELECT INTO command in SQL ServerSELECT INTO command in SQL Server
    the article guides using select into command in sql server with syntax and examples.
  • Hackers break into WordPress serversHackers break into WordPress servers
    an unidentified hacker hacked into the wordpresss server and installed a remote control trojan and download versions of this widely used blog application.
  • Control loop in Unix / LinuxControl loop in Unix / Linux
    the previous chapter has been shown how to create loops and work with them to perform various tasks. sometimes you need to stop a loop or continue their repetitive process.
  • Prncnfg command in WindowsPrncnfg command in Windows
    (applies to windows server (semi-annual channel), windows server 2016, windows server 2012 r2, windows server 2012
  • Manage the Event Log with the command lineManage the Event Log with the command line
    for many of us, using the command line - command line to control and use some of the functions in the windows system will cause them to have headaches and problems. however, if you have to set up an automatic function system in windows and other server operating systems, the command line is an irreplaceable tool ...
  • Moving a website to a new server does not interrupt accessMoving a website to a new server does not interrupt access
    in this article, we will show you how to transfer your website to a new server easily without interrupting the website's access.
  • The command bootcfg delete in WindowsThe command bootcfg delete in Windows
    the bootcfg delete command deletes the operating system entry in the [operating systems] section of the boot.ini file. the command applies to: windows server (semi-annual channel), windows server 2016, windows server 2012 r2, windows server 2012.