{"id":824,"date":"2018-10-03T14:03:07","date_gmt":"2018-10-03T07:03:07","guid":{"rendered":"http:\/\/www.sqlservertutorial.net\/?page_id=824"},"modified":"2020-04-11T20:13:13","modified_gmt":"2020-04-11T13:13:13","slug":"sql-server-while","status":"publish","type":"page","link":"https:\/\/www.sqlservertutorial.net\/sql-server-stored-procedures\/sql-server-while\/","title":{"rendered":"SQL Server WHILE"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you will learn how to use the SQL Server <code>WHILE<\/code> statement to execute a statement block repeatedly based on a specified condition.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='overview-of-while-statement'>Overview of WHILE statement <a href=\"#overview-of-while-statement\" class=\"anchor\" id=\"overview-of-while-statement\" title=\"Anchor for Overview of &lt;code&gt;WHILE&lt;\/code&gt; statement\">#<\/a><\/h2>\n\n\n\n<p>The <code>WHILE<\/code> statement is a control-flow statement that allows you to execute a statement block repeatedly as long as a specified condition is <code>TRUE<\/code>.<\/p>\n\n\n\n<p>The following illustrates the syntax of the <code>WHILE<\/code> statement:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\">WHILE Boolean_expression   \n     { sql_statement | statement_block}  \n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this syntax:<\/p>\n\n\n\n<p>First, the <code>Boolean_expression<\/code> is an expression that evaluates to <code>TRUE<\/code> or <code>FALSE<\/code>.<\/p>\n\n\n\n<p>Second, <code>sql_statement | statement_block<\/code> is any Transact-SQL statement or a set of Transact-SQL statements. A statement block is defined using the <code><a href=\"https:\/\/www.sqlservertutorial.net\/sql-server-stored-procedures\/sql-server-begin-end\/\">BEGIN...END<\/a><\/code> statement.<\/p>\n\n\n\n<p>If the <code>Boolean_expression<\/code> evaluates to <code>FALSE<\/code> when entering the loop, no statement inside the <code>WHILE<\/code> loop will be executed.<\/p>\n\n\n\n<p>Inside the <code>WHILE<\/code> loop, you must change some <a href=\"https:\/\/www.sqlservertutorial.net\/sql-server-stored-procedures\/variables\/\">variables<\/a> to make the <code>Boolean_expression<\/code> returns <code>FALSE<\/code> at some points. Otherwise, you will have an indefinite loop.<\/p>\n\n\n\n<p>Note that if the <code>Boolean_expression<\/code> contains a <code><a href=\"https:\/\/www.sqlservertutorial.net\/sql-server-basics\/sql-server-select\/\">SELECT<\/a><\/code> statement, it must be enclosed in parentheses.<\/p>\n\n\n\n<p>To exit the current iteration of the loop immediately, you use the <code><a href=\"https:\/\/www.sqlservertutorial.net\/sql-server-stored-procedures\/sql-server-break\/\">BREAK<\/a><\/code> statement. To skip the current iteration of the loop and start the new one, you use the <code><a href=\"https:\/\/www.sqlservertutorial.net\/sql-server-stored-procedures\/sql-server-continue\/\">CONTINUE<\/a><\/code> statement.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='sql-server-while-example'>SQL Server WHILE example <a href=\"#sql-server-while-example\" class=\"anchor\" id=\"sql-server-while-example\" title=\"Anchor for SQL Server &lt;code&gt;WHILE&lt;\/code&gt; example\">#<\/a><\/h2>\n\n\n\n<p>Let&#8217;s take an&nbsp;example of using the SQL Server <code>WHILE<\/code> statement to understand it better.<\/p>\n\n\n\n<p>The following example illustrates how to use the <code>WHILE<\/code> statement to print out numbers from 1 to 5:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">DECLARE<\/span> @counter <span class=\"hljs-built_in\">INT<\/span> = <span class=\"hljs-number\">1<\/span>;\n\nWHILE @counter &lt;= 5\n<span class=\"hljs-keyword\">BEGIN<\/span>\n    PRINT @counter;\n    <span class=\"hljs-keyword\">SET<\/span> @counter = @counter + <span class=\"hljs-number\">1<\/span>;\n<span class=\"hljs-keyword\">END<\/span>\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted lang:tsql highlight:0 decode:true\">1\n2\n3\n4\n5<\/code><\/pre>\n\n\n\n<p>In this example:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>First, we declared the <code>@counter<\/code> <a href=\"https:\/\/www.sqlservertutorial.net\/sql-server-stored-procedures\/variables\/\">variable<\/a> and set its value to one.<\/li><li>Then, in the condition of the <code>WHILE<\/code> statement, we checked if the&nbsp;<code>@counter<\/code>is less than or equal to five. If it was not, we printed out the <code>@counter<\/code> and increased its value by one. After five iterations, the <code>@counter<\/code> is 6 which caused the condition of the <code>WHILE<\/code> clause evaluates to <code>FALSE<\/code>, the loop stopped.<\/li><\/ul>\n\n\n\n<p>To learn how to use the <code>WHILE<\/code> loop to process row by row, check it out the <a href=\"https:\/\/www.sqlservertutorial.net\/sql-server-stored-procedures\/sql-server-cursor\/\">cursor<\/a> tutorial.<\/p>\n\n\n\n<p>In this tutorial, you have learned how to use the SQL Server <code>WHILE<\/code> statement to repeat the execution of a statement block based on a specified condition.<\/p>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"824\"\n\t\t\t\tdata-post-url=\"https:\/\/www.sqlservertutorial.net\/sql-server-stored-procedures\/sql-server-while\/\"\n\t\t\t\tdata-post-title=\"SQL Server WHILE\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"824\"\n\t\t\t\tdata-post-url=\"https:\/\/www.sqlservertutorial.net\/sql-server-stored-procedures\/sql-server-while\/\"\n\t\t\t\tdata-post-title=\"SQL Server WHILE\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\n\t\t\t<button class=\"btn btn-primary wth-btn-submit\">Send<\/button>\n\t\t\t<button class=\"btn wth-btn-cancel\">Cancel<\/button>\n\t\t\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial shows you how to use the SQL Server WHILE statement to execute a statement block repeatedly based on a specified condition.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":777,"menu_order":6,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-824","page","type-page","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>SQL Server WHILE - How To Create Loop in SQL Server<\/title>\n<meta name=\"description\" content=\"This tutorial shows you how to use the SQL Server WHILE statement to execute a statement block repeatedly based on a specified condition.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.sqlservertutorial.net\/sql-server-stored-procedures\/sql-server-while\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"SQL Server WHILE - How To Create Loop in SQL Server\" \/>\n<meta property=\"og:description\" content=\"This tutorial shows you how to use the SQL Server WHILE statement to execute a statement block repeatedly based on a specified condition.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.sqlservertutorial.net\/sql-server-stored-procedures\/sql-server-while\/\" \/>\n<meta property=\"og:site_name\" content=\"SQL Server Tutorial\" \/>\n<meta property=\"article:modified_time\" content=\"2020-04-11T13:13:13+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/sql-server-stored-procedures\\\/sql-server-while\\\/\",\"url\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/sql-server-stored-procedures\\\/sql-server-while\\\/\",\"name\":\"SQL Server WHILE - How To Create Loop in SQL Server\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/#website\"},\"datePublished\":\"2018-10-03T07:03:07+00:00\",\"dateModified\":\"2020-04-11T13:13:13+00:00\",\"description\":\"This tutorial shows you how to use the SQL Server WHILE statement to execute a statement block repeatedly based on a specified condition.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/sql-server-stored-procedures\\\/sql-server-while\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.sqlservertutorial.net\\\/sql-server-stored-procedures\\\/sql-server-while\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/sql-server-stored-procedures\\\/sql-server-while\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"SQL Server Stored Procedures\",\"item\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/sql-server-stored-procedures\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"SQL Server WHILE\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/#website\",\"url\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/\",\"name\":\"SQL Server Tutorial\",\"description\":\"The Practical SQL Server Tutorial\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"SQL Server WHILE - How To Create Loop in SQL Server","description":"This tutorial shows you how to use the SQL Server WHILE statement to execute a statement block repeatedly based on a specified condition.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.sqlservertutorial.net\/sql-server-stored-procedures\/sql-server-while\/","og_locale":"en_US","og_type":"article","og_title":"SQL Server WHILE - How To Create Loop in SQL Server","og_description":"This tutorial shows you how to use the SQL Server WHILE statement to execute a statement block repeatedly based on a specified condition.","og_url":"https:\/\/www.sqlservertutorial.net\/sql-server-stored-procedures\/sql-server-while\/","og_site_name":"SQL Server Tutorial","article_modified_time":"2020-04-11T13:13:13+00:00","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.sqlservertutorial.net\/sql-server-stored-procedures\/sql-server-while\/","url":"https:\/\/www.sqlservertutorial.net\/sql-server-stored-procedures\/sql-server-while\/","name":"SQL Server WHILE - How To Create Loop in SQL Server","isPartOf":{"@id":"https:\/\/www.sqlservertutorial.net\/#website"},"datePublished":"2018-10-03T07:03:07+00:00","dateModified":"2020-04-11T13:13:13+00:00","description":"This tutorial shows you how to use the SQL Server WHILE statement to execute a statement block repeatedly based on a specified condition.","breadcrumb":{"@id":"https:\/\/www.sqlservertutorial.net\/sql-server-stored-procedures\/sql-server-while\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.sqlservertutorial.net\/sql-server-stored-procedures\/sql-server-while\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.sqlservertutorial.net\/sql-server-stored-procedures\/sql-server-while\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.sqlservertutorial.net\/"},{"@type":"ListItem","position":2,"name":"SQL Server Stored Procedures","item":"https:\/\/www.sqlservertutorial.net\/sql-server-stored-procedures\/"},{"@type":"ListItem","position":3,"name":"SQL Server WHILE"}]},{"@type":"WebSite","@id":"https:\/\/www.sqlservertutorial.net\/#website","url":"https:\/\/www.sqlservertutorial.net\/","name":"SQL Server Tutorial","description":"The Practical SQL Server Tutorial","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.sqlservertutorial.net\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/www.sqlservertutorial.net\/wp-json\/wp\/v2\/pages\/824","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.sqlservertutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.sqlservertutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.sqlservertutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sqlservertutorial.net\/wp-json\/wp\/v2\/comments?post=824"}],"version-history":[{"count":0,"href":"https:\/\/www.sqlservertutorial.net\/wp-json\/wp\/v2\/pages\/824\/revisions"}],"up":[{"embeddable":true,"href":"https:\/\/www.sqlservertutorial.net\/wp-json\/wp\/v2\/pages\/777"}],"wp:attachment":[{"href":"https:\/\/www.sqlservertutorial.net\/wp-json\/wp\/v2\/media?parent=824"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}