{"id":153,"date":"2019-09-24T07:54:54","date_gmt":"2019-09-24T07:54:54","guid":{"rendered":"http:\/\/sumitjangid.com\/?p=153"},"modified":"2019-09-24T08:03:46","modified_gmt":"2019-09-24T08:03:46","slug":"break","status":"publish","type":"post","link":"http:\/\/sumitjangid.com\/index.php\/2019\/09\/24\/break\/","title":{"rendered":"Break"},"content":{"rendered":"\n<h2>SQL Server&nbsp;<code>BREAK<\/code>&nbsp;statement overview<\/h2>\n\n\n\n<p>In the previous tutorial, you have learned how to use the&nbsp;<code><a href=\"http:\/\/www.sqlservertutorial.net\/sql-server-stored-procedures\/sql-server-while\/\">WHILE<\/a><\/code>&nbsp;statement to create a loop. To exit the current iteration of a loop, you use the&nbsp;<code>BREAK<\/code>&nbsp;statement.<\/p>\n\n\n\n<p>The following illustrates the typical syntax of the&nbsp;<code>BREAK<\/code>&nbsp;statement:<\/p>\n\n\n\n<table class=\"wp-block-table\"><tbody><tr><td>1234567<\/td><td>WHILE Boolean_expressionBEGIN&nbsp;&nbsp;&nbsp;&nbsp;<em>&#8212; statements<\/em>&nbsp;&nbsp; IF condition&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;BREAK;&nbsp;&nbsp;&nbsp;&nbsp;<em>&#8212; other statements&nbsp;&nbsp;&nbsp;&nbsp;<\/em>END<\/td><\/tr><\/tbody><\/table>\n\n\n\n<p>In this syntax, the&nbsp;<code>BREAK<\/code>&nbsp;statement exit the&nbsp;<code>WHILE<\/code>&nbsp;loop immediately once the&nbsp;<code>condition<\/code>&nbsp; specified in the&nbsp;<code><a href=\"http:\/\/www.sqlservertutorial.net\/sql-server-stored-procedures\/sql-server-if-else\/\">IF<\/a><\/code>&nbsp;statement is met. All the statements between the&nbsp;<code>BREAK<\/code>&nbsp;and&nbsp;<code>END<\/code>&nbsp;keywords are skipped.<\/p>\n\n\n\n<p>Suppose we have a&nbsp;<code>WHILE<\/code>&nbsp;loop nested inside another&nbsp;<code>WHILE<\/code>&nbsp;loop:<\/p>\n\n\n\n<table class=\"wp-block-table\"><tbody><tr><td>123456789<\/td><td>WHILE Boolean_expression1BEGIN&nbsp;&nbsp;&nbsp;&nbsp;<em>&#8212; statement<\/em>&nbsp;&nbsp;&nbsp;&nbsp;WHILE Boolean_expression2&nbsp;&nbsp;&nbsp;&nbsp;BEGIN&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;IF condition&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;BREAK;&nbsp;&nbsp;&nbsp;&nbsp;ENDEND<\/td><\/tr><\/tbody><\/table>\n\n\n\n<p>In this case, the&nbsp;<code>BREAK<\/code>&nbsp;statement only exits the innermost loop in the&nbsp;<code>WHILE<\/code>&nbsp;statement.<\/p>\n\n\n\n<p>Note that the&nbsp;<code>BREAK<\/code>&nbsp;statement can be used only inside the&nbsp;<code>WHILE<\/code>&nbsp;loop. The&nbsp;<code>IF<\/code>&nbsp;statement is often used with the&nbsp;<code>BREAK<\/code>&nbsp;statement but it is not required.<\/p>\n\n\n\n<h2>SQL Server&nbsp;<code>BREAK<\/code>&nbsp;statement example<\/h2>\n\n\n\n<p>The following example illustrates how to use the&nbsp;<code>BREAK<\/code>&nbsp;statement:<\/p>\n\n\n\n<table class=\"wp-block-table\"><tbody><tr><td>123456789<\/td><td>DECLARE @counter INT = 0;&nbsp;WHILE @counter &lt;= 5BEGIN&nbsp;&nbsp;&nbsp;&nbsp;SET @counter = @counter + 1;&nbsp;&nbsp;&nbsp;&nbsp;IF @counter = 4&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;BREAK;&nbsp;&nbsp;&nbsp;&nbsp;PRINT @counter;END<\/td><\/tr><\/tbody><\/table>\n\n\n\n<p>In this example:<\/p>\n\n\n\n<p>First, we&nbsp;<a href=\"http:\/\/www.sqlservertutorial.net\/sql-server-stored-procedures\/variables\/\">declared a variable<\/a>&nbsp;named&nbsp;<code>@counter<\/code>&nbsp;and set its value to zero.<\/p>\n\n\n\n<p>Then, we used the&nbsp;<code>WHILE<\/code>&nbsp;statement to increases the&nbsp;<code>@counter<\/code>&nbsp;by one in each iteration and print out the&nbsp;<code>@counter<\/code>\u2018s value as long as the value of the&nbsp;<code>@counter<\/code>&nbsp;is less than or equal to five.<\/p>\n\n\n\n<p>Inside the loop, we also checked if the value of&nbsp;<code>@counter<\/code>&nbsp;equals four, then we exited the loop. In the fourth iteration, the value of the counter reached 4, then the loop is terminated. Also, the&nbsp;<code>PRINT<\/code>&nbsp;statement after the&nbsp;<code>BREAK<\/code>&nbsp;statement was skipped.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>SQL Server&nbsp;BREAK&nbsp;statement overview In the previous tutorial, you have learned how to use the&nbsp;WHILE&nbsp;statement to create a loop. To exit the current iteration of a loop, you use the&nbsp;BREAK&nbsp;statement. The following illustrates the typical syntax&hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[2,18,19],"tags":[],"_links":{"self":[{"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/posts\/153"}],"collection":[{"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/comments?post=153"}],"version-history":[{"count":1,"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/posts\/153\/revisions"}],"predecessor-version":[{"id":154,"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/posts\/153\/revisions\/154"}],"wp:attachment":[{"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/media?parent=153"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/categories?post=153"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/tags?post=153"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}