AskHandle

AskHandle Blog

How to Use Foreach Loop in MSSQL?

July 11, 2025Lillian Kim3 min read

How to Use Foreach Loop in MSSQL?

If you are familiar with using MSSQL databases, you may have come across the term "foreach loop." This common feature in MSSQL allows you to iterate through a result set or a collection of items seamlessly. In this guide, we will walk you through the basics of using foreach loops in MSSQL, address common questions, and provide practical examples to help you understand how to leverage this functionality effectively.

What is a Foreach Loop in MSSQL?

A foreach loop in MSSQL, often referred to as a cursor, is a programming construct that enables you to iterate over a set of rows returned by a query. It allows you to perform operations on each row individually, making it a powerful tool for processing data sequentially. While it may not always be the most efficient method in terms of performance, there are scenarios where foreach loops can be useful, such as when you need to perform row-by-row operations or complex data manipulations.

How to Create a Foreach Loop in MSSQL?

To create a foreach loop in MSSQL, you typically use a CURSOR statement. Here's a simple example to illustrate how you can set up a basic foreach loop:

sql
1DECLARE @variable_name data_type;
2DECLARE cursor_name CURSOR FOR
3SELECT column_name FROM table_name;
4
5OPEN cursor_name;
6FETCH NEXT FROM cursor_name INTO @variable_name;
7
8WHILE @@FETCH_STATUS = 0
9BEGIN
10    -- Perform operations on @variable_name
11    FETCH NEXT FROM cursor_name INTO @variable_name;
12END
13
14CLOSE cursor_name;
15DEALLOCATE cursor_name;

In this example:

  • @variable_name represents the variable where each row value will be stored.
  • cursor_name is the name of the cursor defined for the query result set.
  • FETCH NEXT retrieves the next row into the variable until there are no more rows to fetch.

When to Use a Foreach Loop in MSSQL?

While foreach loops can be handy in certain situations, it's crucial to use them judiciously. Here are some scenarios where foreach loops in MSSQL can be beneficial:

  1. Row-by-Row Processing: When you need to perform operations on individual rows that cannot be accomplished with a single query.

  2. Complex Data Transformations: For scenarios where you need to transform data iteratively based on certain conditions.

  3. Cursor Over Multiple Tables: When you need to process data from multiple tables in a specific order that cannot be achieved with a single query.

Practical Example of Using a Foreach Loop in MSSQL

Let's consider a practical example where you want to update the salary of employees in a table based on certain criteria. Here's how you can achieve this using a foreach loop:

sql
1DECLARE @emp_id INT;
2DECLARE @new_salary DECIMAL(10, 2);
3
4DECLARE emp_cursor CURSOR FOR
5SELECT employee_id, current_salary FROM employees;
6
7OPEN emp_cursor;
8FETCH NEXT FROM emp_cursor INTO @emp_id, @new_salary;
9
10WHILE @@FETCH_STATUS = 0
11BEGIN
12    IF @new_salary < 50000
13    BEGIN
14        UPDATE employees
15        SET current_salary = @new_salary * 1.1
16        WHERE employee_id = @emp_id;
17    END
18
19    FETCH NEXT FROM emp_cursor INTO @emp_id, @new_salary;
20END
21
22CLOSE emp_cursor;
23DEALLOCATE emp_cursor;

In this example, we use a cursor to iterate over each employee's current salary and adjust it based on a condition.

By understanding the basics of foreach loops in MSSQL and when to use them effectively, you can enhance your data processing capabilities and perform intricate operations with ease.