logo

SQL サーバーの WHILE LOOP

この記事では、SQL Server での WHILE ループの使用の完全な概要について説明します。あ WHILE ループは、指定された条件が満たされるまで一連のステートメントを繰り返し実行するために使用される制御フロー ステートメントです。 。このループは指定された条件で始まり、それを評価し、それが TRUE の場合、ステートメントはループ内に入ってさらに実行されます。条件が FALSE になると実行されません。これは、SQL Server の while ループが 0 回以上実行される可能性があることを意味します。

WHILEループのフローチャート

次のフローチャートは、SQL Server 内の WHILE ループの完全なワークフローを説明します。

xor c++
SQL サーバーの WHILE LOOP

このグラフから、指定された条件が反復ごとにチェックされ、評価結果に基づいてコード フローが決定されることがわかります。結果が TRUE と評価された場合、制御フローはループ内に入り、さらに実行されます。評価結果が FALSE の場合、制御フローはループを終了し、ループ外のステートメントまたはクエリが実行されます。

構文

次の構文は、SQL Server の WHILE ループを示しています。

 WHILE boolean_condition BEGIN BREAK END; 

この構文には、次のパラメータまたは引数があります。

    ブール条件:これは必須の条件であり、反復ごとにテストされて TRUE または FALSE の結果が返されます。 SELECT ステートメントの場合は、括弧で囲む必要があります。sql_statement または statement_block:SQL ステートメントまたはグループ化は、BEGIN および END キーワード内で定義されます。ループが FALSE になるまで、各反復で実行されます。壊す:最も内側のループが即座に終了し、制御フローはループの次のステートメントから再開されます。続く:ループ内の残りのステートメントをスキップせずに、次の反復にジャンプします。通常、ループが最初から再開されます。

WHILE ループの例

例を通して、SQL Server で WHILE ループがどのように機能するかを理解しましょう。与えられた例では、最初に次の値を宣言しました。 整数型 次に、WHILE ループで条件がチェックされ、条件が満たされているかどうかがチェックされます。 真実 を指定すると、print ステートメントが出力されます。ループになると、 間違い 、WHILE ループの後の次のステートメントが出力されます。

charからintへ
 DECLARE @stud_value INT; SET @stud_value = 1; WHILE @stud_value <= 5 begin print 'mark henry'; set @stud_value="@stud_value" + 1; end; 'rose bennet'; < pre> <p>Executing this statement will return the following output:</p> <img src="//techcodeview.com/img/sql-server-tutorials/44/sql-server-while-loop-2.webp" alt="SQL Server WHILE LOOP"> <p>In the above WHILE loop code snippet, we must increment the variable&apos;s value after each iteration. See the below part of the above code line as <strong>SET @stud_value = @stud_value + 1</strong> . If we do not write this statement, the loop will execute infinitely because it cannot becomes FALSE.</p> <pre> BEGIN PRINT &apos;Mark Henry&apos;; SET @stud_value = @stud_value + 1; END; </pre> <h3>Infinite WHILE Loop</h3> <p>An infinite loop occurs when the evaluation of a condition will never be false. Therefore, the loop will never end and be executed forever. The loop in the following code snippet is infinite because the variable&apos;s value is not incremented.</p> <pre> DECLARE @stud_value INT; SET @stud_value = 1; WHILE @stud_value <= 5 begin print 'please stop execution!' end; < pre> <p>Executing the loop will display the below output. This loop will never end its execution until we do not cancel their execution of the query manually.</p> <img src="//techcodeview.com/img/sql-server-tutorials/44/sql-server-while-loop-3.webp" alt="SQL Server WHILE LOOP"> <h3>Inserting Records with WHILE Loop</h3> <p>We can also use the WHILE loop to insert records into the defined table. Let us see how to inserts dummy records into the database. First, we will create a table named <strong>&apos;bikeshop&apos;</strong> containing three columns: <strong>Id, bike_name,</strong> and <strong>price</strong> . Execute the following statement to create this table:</p> <pre> CREATE TABLE bikeshop ( Id INT PRIMARY KEY IDENTITY, bike_name VARCHAR (50) NOT NULL, price FLOAT ) </pre> <p>Next, we will use the WHILE loop to insert ten records into this table by executing the following script:</p> <pre> DECLARE @count INT; SET @count = 1; WHILE @count <= 10 begin insert into bikeshop values('bike-' + cast(@count as varchar), @count*5000) set @count="@count" 1; end; < pre> <p>In this code, we have declared a variable @ <strong>count</strong> and then initialize its value with 1 using a SET clause. Next, we have to define the loop body that executes the INSERT statement to add one record in each execution. The <strong>bike_name column</strong> will append the value of a @count variable with the string <strong>Bike</strong> , and the <strong>price</strong> column determines by the value of a @count variable multiplied by <strong>5000</strong> . The loop will execute until the value of the @count variable becomes FALSE. It means the WHILE loop will execute ten times and <strong>inserts ten records</strong> into the table bikeshop.</p> <p>Now, we can verify all the records of the bikeshop table with the SELECT statement. It will display the following output:</p> <img src="//techcodeview.com/img/sql-server-tutorials/44/sql-server-while-loop-4.webp" alt="SQL Server WHILE LOOP"> <h3>BREAK Statement</h3> <p>SQL Server also allows us to use the BREAK statement in the WHILE loop like programming languages. This statement is used to <strong>immediately stop the current iteration of the loop</strong> , and control flow resumes with the next statement after the loop. In general, we will use the <a href="/sql-server-if-else"> <strong>IF...ELSE statement</strong> </a> to check whether or not a condition has occurred.</p> <p>The following example will explain how to use the BREAK statement in the WHILE loop:</p> <pre> DECLARE @count INT; SET @count = 1; WHILE @count = 6 BEGIN BREAK END SET @Count = @Count + 1 END; </pre> <p>Executing the code will display the below output:</p> <img src="//techcodeview.com/img/sql-server-tutorials/44/sql-server-while-loop-5.webp" alt="SQL Server WHILE LOOP"> <p>The value of the variable is first evaluated in this code. If it is TRUE, the control enters into the loop and prints the statement. When the variable value is greater than or equal to 6, control enters the IF...ELSE block and executes the BREAK statement to terminate the loop. If an IF...ELSE block fails to meet the condition; then, the loop will keep running until the condition is changed to FALSE.</p> <h3>CONTINUE Statement</h3> <p>SQL Server also allows us to use the CONTINUE statement in the WHILE loop like programming languages. This statement immediately <strong>terminates the current execution of the loop when the specified condition is met</strong> , and control flow returns to the beginning of the loop. In general, the IF...ELSE statement will be used to test whether or not a condition has been met.</p> <p>The CONTINUE statement in the WHILE loop is demonstrated in the following example. In this example, we&apos;ll assume that we wish to use a WHILE loop to <strong>print only odd values</strong> . The CONTINUE statement can be used to do this. This example will first <strong>test</strong> whether the variable value is <strong>odd or even</strong> . If it is even, the execution goes inside the IF&#x2026;ELSE statement blocks and decrement the variable value by one. Then, it will execute the CONTINUE statement and starts a new iteration from the beginning.</p> <pre> DECLARE @Count INT SET @Count = 1 WHILE (@Count <= 1 2="0" 44 20) begin if @count % set + continue end print 'the odd value is=" + CONVERT(VARCHAR, @Count) SET @Count = @Count + 1 END &lt;/pre&gt; &lt;p&gt;Executing the code snippet will display the below output:&lt;/p&gt; &lt;img src=" techcodeview.com img sql-server-tutorials sql-server-while-loop-6.webp' alt="SQL Server WHILE LOOP"> <h3>How to implementing paging with WHILE loop in SQL Server?</h3> <p>We can also use the WHILE loop for implementing the paging. Paging allows displaying the subset of records from a table at any particular time. The following example will explain this concept. The WHILE loop in the code will select two records from the bikeshop table at a time. The records that have been chosen are then displayed in the output.</p> <pre> DECLARE @count INT DECLARE @limit INT; SET @count = 0 SET @limit = 2; WHILE @count <10 begin select * from bikeshop order by id offset @count rows fetch next @limit only set + 2; end; < pre> <p>Executing the code snippet will return the below output:</p> <img src="//techcodeview.com/img/sql-server-tutorials/44/sql-server-while-loop-7.webp" alt="SQL Server WHILE LOOP"> <h3>Nested WHILE Loop</h3> <p>The Nested WHILE Loop in SQL Server is simply a WHILE Loop written inside another WHILE Loop. When we work on multi-layered data, the Nested WHILE loops are essential. Because this concept is useful in extracting the layered data when we want to select them, it is recommended to be careful while using the nested loop.</p> <p> <strong>Syntax</strong> </p> <p>The following syntax illustrate the working of the nested WHILE Loop in SQL Server:</p> <pre> WHILE Expression BEGIN WHILE @Val2 <= 10 begin --second while loop statements sql end --this statement is outside the second --which first -- this < pre> <p>Let us explain this syntax step by step:</p> <p> <strong>Step 1:</strong> The loop starts by checking the first WHILE loop condition, and if it finds a false result, it will exit from While Loop. Otherwise, if the result is true, the control goes inside the BEGIN and END block for further execution. This block will start the execution of the second WHILE loop. See step 2.</p> <p> <strong>Step 2:</strong> This step will check the condition in the Nested WHILE Loop, and if it is false, the second loop will be exit and execute the statement outside this. Otherwise, if the result is true, the control goes inside the BEGIN and END block for further execution.</p> <p> <strong>Step 3:</strong> Once all the statements execute from the second WHILE loop, the control goes to the first WHILE and repeats the first step.</p> <p> <strong>Example</strong> </p> <p>The following example will print the multiplication table of 5 up to 10 using the nested WHILE loop.</p> <pre> DECLARE @val1 INT DECLARE @val2 INT SET @val1 = 5 SET @val2 = 1 WHILE @val1 <= 5 44 begin while @val2 <="10" print convert(varchar, @val1) + ' * @val2) techcodeview.com img sql-server-tutorials sql-server-while-loop-8.webp' alt="SQL Server WHILE LOOP"> <h2>Conclusion</h2> <p>The WHILE loop is a useful method when there is a need to execute a SQL script repeatedly. The article explained how to work with the WHILE loop in MS SQL Server to execute operations such as record insertion and pagination with a simple example. Here we have also learned the BREAK and CONTINUE statements to control the WHILE loop iteration.</p> <hr></=></pre></=></pre></10></pre></=></pre></=></pre></=></pre></=>

無限WHILEループ

無限ループは、条件の評価が決して false にならない場合に発生します。したがって、ループは決して終了せず、永久に実行されます。次のコード スニペットのループは、変数の値がインクリメントされないため、無限になります。

 DECLARE @stud_value INT; SET @stud_value = 1; WHILE @stud_value <= 5 begin print \'please stop execution!\' end; < pre> <p>Executing the loop will display the below output. This loop will never end its execution until we do not cancel their execution of the query manually.</p> <img src="//techcodeview.com/img/sql-server-tutorials/44/sql-server-while-loop-3.webp" alt="SQL Server WHILE LOOP"> <h3>Inserting Records with WHILE Loop</h3> <p>We can also use the WHILE loop to insert records into the defined table. Let us see how to inserts dummy records into the database. First, we will create a table named <strong>&apos;bikeshop&apos;</strong> containing three columns: <strong>Id, bike_name,</strong> and <strong>price</strong> . Execute the following statement to create this table:</p> <pre> CREATE TABLE bikeshop ( Id INT PRIMARY KEY IDENTITY, bike_name VARCHAR (50) NOT NULL, price FLOAT ) </pre> <p>Next, we will use the WHILE loop to insert ten records into this table by executing the following script:</p> <pre> DECLARE @count INT; SET @count = 1; WHILE @count <= 10 begin insert into bikeshop values(\'bike-\' + cast(@count as varchar), @count*5000) set @count="@count" 1; end; < pre> <p>In this code, we have declared a variable @ <strong>count</strong> and then initialize its value with 1 using a SET clause. Next, we have to define the loop body that executes the INSERT statement to add one record in each execution. The <strong>bike_name column</strong> will append the value of a @count variable with the string <strong>Bike</strong> , and the <strong>price</strong> column determines by the value of a @count variable multiplied by <strong>5000</strong> . The loop will execute until the value of the @count variable becomes FALSE. It means the WHILE loop will execute ten times and <strong>inserts ten records</strong> into the table bikeshop.</p> <p>Now, we can verify all the records of the bikeshop table with the SELECT statement. It will display the following output:</p> <img src="//techcodeview.com/img/sql-server-tutorials/44/sql-server-while-loop-4.webp" alt="SQL Server WHILE LOOP"> <h3>BREAK Statement</h3> <p>SQL Server also allows us to use the BREAK statement in the WHILE loop like programming languages. This statement is used to <strong>immediately stop the current iteration of the loop</strong> , and control flow resumes with the next statement after the loop. In general, we will use the <a href="/sql-server-if-else"> <strong>IF...ELSE statement</strong> </a> to check whether or not a condition has occurred.</p> <p>The following example will explain how to use the BREAK statement in the WHILE loop:</p> <pre> DECLARE @count INT; SET @count = 1; WHILE @count = 6 BEGIN BREAK END SET @Count = @Count + 1 END; </pre> <p>Executing the code will display the below output:</p> <img src="//techcodeview.com/img/sql-server-tutorials/44/sql-server-while-loop-5.webp" alt="SQL Server WHILE LOOP"> <p>The value of the variable is first evaluated in this code. If it is TRUE, the control enters into the loop and prints the statement. When the variable value is greater than or equal to 6, control enters the IF...ELSE block and executes the BREAK statement to terminate the loop. If an IF...ELSE block fails to meet the condition; then, the loop will keep running until the condition is changed to FALSE.</p> <h3>CONTINUE Statement</h3> <p>SQL Server also allows us to use the CONTINUE statement in the WHILE loop like programming languages. This statement immediately <strong>terminates the current execution of the loop when the specified condition is met</strong> , and control flow returns to the beginning of the loop. In general, the IF...ELSE statement will be used to test whether or not a condition has been met.</p> <p>The CONTINUE statement in the WHILE loop is demonstrated in the following example. In this example, we&apos;ll assume that we wish to use a WHILE loop to <strong>print only odd values</strong> . The CONTINUE statement can be used to do this. This example will first <strong>test</strong> whether the variable value is <strong>odd or even</strong> . If it is even, the execution goes inside the IF&#x2026;ELSE statement blocks and decrement the variable value by one. Then, it will execute the CONTINUE statement and starts a new iteration from the beginning.</p> <pre> DECLARE @Count INT SET @Count = 1 WHILE (@Count <= 1 2="0" 44 20) begin if @count % set + continue end print \'the odd value is=" + CONVERT(VARCHAR, @Count) SET @Count = @Count + 1 END &lt;/pre&gt; &lt;p&gt;Executing the code snippet will display the below output:&lt;/p&gt; &lt;img src=" techcodeview.com img sql-server-tutorials sql-server-while-loop-6.webp\' alt="SQL Server WHILE LOOP"> <h3>How to implementing paging with WHILE loop in SQL Server?</h3> <p>We can also use the WHILE loop for implementing the paging. Paging allows displaying the subset of records from a table at any particular time. The following example will explain this concept. The WHILE loop in the code will select two records from the bikeshop table at a time. The records that have been chosen are then displayed in the output.</p> <pre> DECLARE @count INT DECLARE @limit INT; SET @count = 0 SET @limit = 2; WHILE @count <10 begin select * from bikeshop order by id offset @count rows fetch next @limit only set + 2; end; < pre> <p>Executing the code snippet will return the below output:</p> <img src="//techcodeview.com/img/sql-server-tutorials/44/sql-server-while-loop-7.webp" alt="SQL Server WHILE LOOP"> <h3>Nested WHILE Loop</h3> <p>The Nested WHILE Loop in SQL Server is simply a WHILE Loop written inside another WHILE Loop. When we work on multi-layered data, the Nested WHILE loops are essential. Because this concept is useful in extracting the layered data when we want to select them, it is recommended to be careful while using the nested loop.</p> <p> <strong>Syntax</strong> </p> <p>The following syntax illustrate the working of the nested WHILE Loop in SQL Server:</p> <pre> WHILE Expression BEGIN WHILE @Val2 <= 10 begin --second while loop statements sql end --this statement is outside the second --which first -- this < pre> <p>Let us explain this syntax step by step:</p> <p> <strong>Step 1:</strong> The loop starts by checking the first WHILE loop condition, and if it finds a false result, it will exit from While Loop. Otherwise, if the result is true, the control goes inside the BEGIN and END block for further execution. This block will start the execution of the second WHILE loop. See step 2.</p> <p> <strong>Step 2:</strong> This step will check the condition in the Nested WHILE Loop, and if it is false, the second loop will be exit and execute the statement outside this. Otherwise, if the result is true, the control goes inside the BEGIN and END block for further execution.</p> <p> <strong>Step 3:</strong> Once all the statements execute from the second WHILE loop, the control goes to the first WHILE and repeats the first step.</p> <p> <strong>Example</strong> </p> <p>The following example will print the multiplication table of 5 up to 10 using the nested WHILE loop.</p> <pre> DECLARE @val1 INT DECLARE @val2 INT SET @val1 = 5 SET @val2 = 1 WHILE @val1 <= 5 44 begin while @val2 <="10" print convert(varchar, @val1) + \' * @val2) techcodeview.com img sql-server-tutorials sql-server-while-loop-8.webp\' alt="SQL Server WHILE LOOP"> <h2>Conclusion</h2> <p>The WHILE loop is a useful method when there is a need to execute a SQL script repeatedly. The article explained how to work with the WHILE loop in MS SQL Server to execute operations such as record insertion and pagination with a simple example. Here we have also learned the BREAK and CONTINUE statements to control the WHILE loop iteration.</p> <hr></=></pre></=></pre></10></pre></=></pre></=></pre></=>

次に、WHILE ループを使用して次のスクリプトを実行し、10 個のレコードをこのテーブルに挿入します。

 DECLARE @count INT; SET @count = 1; WHILE @count <= 10 begin insert into bikeshop values(\'bike-\' + cast(@count as varchar), @count*5000) set @count="@count" 1; end; < pre> <p>In this code, we have declared a variable @ <strong>count</strong> and then initialize its value with 1 using a SET clause. Next, we have to define the loop body that executes the INSERT statement to add one record in each execution. The <strong>bike_name column</strong> will append the value of a @count variable with the string <strong>Bike</strong> , and the <strong>price</strong> column determines by the value of a @count variable multiplied by <strong>5000</strong> . The loop will execute until the value of the @count variable becomes FALSE. It means the WHILE loop will execute ten times and <strong>inserts ten records</strong> into the table bikeshop.</p> <p>Now, we can verify all the records of the bikeshop table with the SELECT statement. It will display the following output:</p> <img src="//techcodeview.com/img/sql-server-tutorials/44/sql-server-while-loop-4.webp" alt="SQL Server WHILE LOOP"> <h3>BREAK Statement</h3> <p>SQL Server also allows us to use the BREAK statement in the WHILE loop like programming languages. This statement is used to <strong>immediately stop the current iteration of the loop</strong> , and control flow resumes with the next statement after the loop. In general, we will use the <a href="/sql-server-if-else"> <strong>IF...ELSE statement</strong> </a> to check whether or not a condition has occurred.</p> <p>The following example will explain how to use the BREAK statement in the WHILE loop:</p> <pre> DECLARE @count INT; SET @count = 1; WHILE @count = 6 BEGIN BREAK END SET @Count = @Count + 1 END; </pre> <p>Executing the code will display the below output:</p> <img src="//techcodeview.com/img/sql-server-tutorials/44/sql-server-while-loop-5.webp" alt="SQL Server WHILE LOOP"> <p>The value of the variable is first evaluated in this code. If it is TRUE, the control enters into the loop and prints the statement. When the variable value is greater than or equal to 6, control enters the IF...ELSE block and executes the BREAK statement to terminate the loop. If an IF...ELSE block fails to meet the condition; then, the loop will keep running until the condition is changed to FALSE.</p> <h3>CONTINUE Statement</h3> <p>SQL Server also allows us to use the CONTINUE statement in the WHILE loop like programming languages. This statement immediately <strong>terminates the current execution of the loop when the specified condition is met</strong> , and control flow returns to the beginning of the loop. In general, the IF...ELSE statement will be used to test whether or not a condition has been met.</p> <p>The CONTINUE statement in the WHILE loop is demonstrated in the following example. In this example, we&apos;ll assume that we wish to use a WHILE loop to <strong>print only odd values</strong> . The CONTINUE statement can be used to do this. This example will first <strong>test</strong> whether the variable value is <strong>odd or even</strong> . If it is even, the execution goes inside the IF&#x2026;ELSE statement blocks and decrement the variable value by one. Then, it will execute the CONTINUE statement and starts a new iteration from the beginning.</p> <pre> DECLARE @Count INT SET @Count = 1 WHILE (@Count <= 1 2="0" 44 20) begin if @count % set + continue end print \'the odd value is=" + CONVERT(VARCHAR, @Count) SET @Count = @Count + 1 END &lt;/pre&gt; &lt;p&gt;Executing the code snippet will display the below output:&lt;/p&gt; &lt;img src=" techcodeview.com img sql-server-tutorials sql-server-while-loop-6.webp\' alt="SQL Server WHILE LOOP"> <h3>How to implementing paging with WHILE loop in SQL Server?</h3> <p>We can also use the WHILE loop for implementing the paging. Paging allows displaying the subset of records from a table at any particular time. The following example will explain this concept. The WHILE loop in the code will select two records from the bikeshop table at a time. The records that have been chosen are then displayed in the output.</p> <pre> DECLARE @count INT DECLARE @limit INT; SET @count = 0 SET @limit = 2; WHILE @count <10 begin select * from bikeshop order by id offset @count rows fetch next @limit only set + 2; end; < pre> <p>Executing the code snippet will return the below output:</p> <img src="//techcodeview.com/img/sql-server-tutorials/44/sql-server-while-loop-7.webp" alt="SQL Server WHILE LOOP"> <h3>Nested WHILE Loop</h3> <p>The Nested WHILE Loop in SQL Server is simply a WHILE Loop written inside another WHILE Loop. When we work on multi-layered data, the Nested WHILE loops are essential. Because this concept is useful in extracting the layered data when we want to select them, it is recommended to be careful while using the nested loop.</p> <p> <strong>Syntax</strong> </p> <p>The following syntax illustrate the working of the nested WHILE Loop in SQL Server:</p> <pre> WHILE Expression BEGIN WHILE @Val2 <= 10 begin --second while loop statements sql end --this statement is outside the second --which first -- this < pre> <p>Let us explain this syntax step by step:</p> <p> <strong>Step 1:</strong> The loop starts by checking the first WHILE loop condition, and if it finds a false result, it will exit from While Loop. Otherwise, if the result is true, the control goes inside the BEGIN and END block for further execution. This block will start the execution of the second WHILE loop. See step 2.</p> <p> <strong>Step 2:</strong> This step will check the condition in the Nested WHILE Loop, and if it is false, the second loop will be exit and execute the statement outside this. Otherwise, if the result is true, the control goes inside the BEGIN and END block for further execution.</p> <p> <strong>Step 3:</strong> Once all the statements execute from the second WHILE loop, the control goes to the first WHILE and repeats the first step.</p> <p> <strong>Example</strong> </p> <p>The following example will print the multiplication table of 5 up to 10 using the nested WHILE loop.</p> <pre> DECLARE @val1 INT DECLARE @val2 INT SET @val1 = 5 SET @val2 = 1 WHILE @val1 <= 5 44 begin while @val2 <="10" print convert(varchar, @val1) + \' * @val2) techcodeview.com img sql-server-tutorials sql-server-while-loop-8.webp\' alt="SQL Server WHILE LOOP"> <h2>Conclusion</h2> <p>The WHILE loop is a useful method when there is a need to execute a SQL script repeatedly. The article explained how to work with the WHILE loop in MS SQL Server to execute operations such as record insertion and pagination with a simple example. Here we have also learned the BREAK and CONTINUE statements to control the WHILE loop iteration.</p> <hr></=></pre></=></pre></10></pre></=></pre></=>

コードを実行すると、次の出力が表示されます。

比較可能なリスト
SQL サーバーの WHILE LOOP

このコードでは、変数の値が最初に評価されます。 TRUE の場合、コントロールはループに入り、ステートメントを出力します。変数値が 6 以上の場合、制御は IF...ELSE ブロックに入り、BREAK ステートメントを実行してループを終了します。 IF...ELSE ブロックが条件を満たさない場合。その後、条件が FALSE に変更されるまでループが実行され続けます。

CONTINUE ステートメント

SQL Server では、プログラミング言語と同様に、WHILE ループで CONTINUE ステートメントを使用することもできます。この発言は即座に 指定された条件が満たされると、ループの現在の実行を終了します。 、制御フローはループの先頭に戻ります。一般に、IF...ELSE ステートメントは、条件が満たされているかどうかをテストするために使用されます。

WHILE ループ内の CONTINUE ステートメントを次の例に示します。この例では、WHILE ループを使用して次のことを行うと仮定します。 奇数の値のみを出力します 。これを行うには CONTINUE ステートメントを使用できます。この例では、まず、 テスト 変数の値が 奇数または偶数 。偶数の場合、実行は IF…ELSE ステートメント ブロック内に入り、変数値を 1 つ減らします。次に、CONTINUE ステートメントが実行され、最初から新しい反復が開始されます。

 DECLARE @Count INT SET @Count = 1 WHILE (@Count <= 1 2="0" 44 20) begin if @count % set + continue end print \'the odd value is=" + CONVERT(VARCHAR, @Count) SET @Count = @Count + 1 END &lt;/pre&gt; &lt;p&gt;Executing the code snippet will display the below output:&lt;/p&gt; &lt;img src=" techcodeview.com img sql-server-tutorials sql-server-while-loop-6.webp\' alt="SQL Server WHILE LOOP"> <h3>How to implementing paging with WHILE loop in SQL Server?</h3> <p>We can also use the WHILE loop for implementing the paging. Paging allows displaying the subset of records from a table at any particular time. The following example will explain this concept. The WHILE loop in the code will select two records from the bikeshop table at a time. The records that have been chosen are then displayed in the output.</p> <pre> DECLARE @count INT DECLARE @limit INT; SET @count = 0 SET @limit = 2; WHILE @count <10 begin select * from bikeshop order by id offset @count rows fetch next @limit only set + 2; end; < pre> <p>Executing the code snippet will return the below output:</p> <img src="//techcodeview.com/img/sql-server-tutorials/44/sql-server-while-loop-7.webp" alt="SQL Server WHILE LOOP"> <h3>Nested WHILE Loop</h3> <p>The Nested WHILE Loop in SQL Server is simply a WHILE Loop written inside another WHILE Loop. When we work on multi-layered data, the Nested WHILE loops are essential. Because this concept is useful in extracting the layered data when we want to select them, it is recommended to be careful while using the nested loop.</p> <p> <strong>Syntax</strong> </p> <p>The following syntax illustrate the working of the nested WHILE Loop in SQL Server:</p> <pre> WHILE Expression BEGIN WHILE @Val2 <= 10 begin --second while loop statements sql end --this statement is outside the second --which first -- this < pre> <p>Let us explain this syntax step by step:</p> <p> <strong>Step 1:</strong> The loop starts by checking the first WHILE loop condition, and if it finds a false result, it will exit from While Loop. Otherwise, if the result is true, the control goes inside the BEGIN and END block for further execution. This block will start the execution of the second WHILE loop. See step 2.</p> <p> <strong>Step 2:</strong> This step will check the condition in the Nested WHILE Loop, and if it is false, the second loop will be exit and execute the statement outside this. Otherwise, if the result is true, the control goes inside the BEGIN and END block for further execution.</p> <p> <strong>Step 3:</strong> Once all the statements execute from the second WHILE loop, the control goes to the first WHILE and repeats the first step.</p> <p> <strong>Example</strong> </p> <p>The following example will print the multiplication table of 5 up to 10 using the nested WHILE loop.</p> <pre> DECLARE @val1 INT DECLARE @val2 INT SET @val1 = 5 SET @val2 = 1 WHILE @val1 <= 5 44 begin while @val2 <="10" print convert(varchar, @val1) + \' * @val2) techcodeview.com img sql-server-tutorials sql-server-while-loop-8.webp\' alt="SQL Server WHILE LOOP"> <h2>Conclusion</h2> <p>The WHILE loop is a useful method when there is a need to execute a SQL script repeatedly. The article explained how to work with the WHILE loop in MS SQL Server to execute operations such as record insertion and pagination with a simple example. Here we have also learned the BREAK and CONTINUE statements to control the WHILE loop iteration.</p> <hr></=></pre></=></pre></10></pre></=>