While loop: In SQL SERVER, while loop can be used in similar manner as any other programming language. A while loop will check the condition first and then execute the block of SQL Statements within it as long as the condition evaluates true.
Syntax:
WHILE condition
BEGIN
{...statements...}
END;
Parameters:
1. Condition: The condition is tested in each pass through the loop. If condition evaluates to TRUE, the loop body is executed otherwise the loop is terminated.
2. Statements: The statements that needs to be executed in each pass through the loop.
Example:

Output:

Break statement: BREAK statement as the name signifies is used to break the flow of control. It can be used in SQL in similar manner as any other programming language.
Example: While loop with Break statement

Output:

Note : In the example, when variables value became five, BREAK Statement is executed and the control gets out from the Loop.
Do-While loop: SQL server does not have the feature of do-while loop but by doing little modifications in while loop, the same behaviour can be achieved.
Example 1:

Output:

Example 2:

Output:

CASE statement: In SQL Server, the CASE statement has the same functionality as IF-THEN-ELSE statement.
Syntax:
CASE Expression WHEN Con_1 THEN Output1 WHEN Con_2 THEN Output2 WHEN Con_3 THEN Output3 WHEN Con_4 THEN Output4 ... WHEN Con_n THEN Outputn ELSE output END
Paramaters:
1. Expression: The value to be compared to the list of conditions(Optional).
2. Con_1, Con_2, …Con_n: The conditions are required and are evaluated in the order they are listed. Once a condition is true, the CASE function will return the result and not evaluate the conditions any further.
3. Output1, Output2, …Outputn: The output to be printed once the condition evaluates true.
Example:

Output:

Recommended Posts:
- Mitigation of SQL Injection Attack using Prepared Statements (Parameterized Queries)
- Difference between Structured Query Language (SQL) and Transact-SQL (T-SQL)
- SQL | Conditional Expressions
- Delete statement in MS SQL Server
- SQL Server Mathematical functions (SQRT, PI, SQUARE, ROUND, CEILING & FLOOR)
- SQL Server Identity
- SQL Server | STUFF() Function
- SQL Server | SERVERPROPERTY()
- Mean and Mode in SQL Server
- Allow only alphabets in column in SQL Server
- SQL SERVER | IN Condition
- Difference between MySQL and MS SQL Server
- Comparisons between Oracle vs SQL Server
- Introduction of MS SQL Server
- Select statement in MS SQL Server
- Order by in MS SQL Server
- Distinct clause in MS SQL Server
- Where clause in MS SQL Server
- Difference between Couchbase and MS SQL Server
- Create, Alter and Drop schema in MS SQL Server
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

