Search⌘ K

Cursors

Understand how to implement T-SQL cursors to process database rows one at a time. This lesson helps you master cursor declaration, opening, fetching, looping, and closing, providing practical skills for row-by-row data manipulation within SQL Server.

We'll cover the following...

Data manipulation commands like SELECT, UPDATE, and DELETE always work with groups of rows. We can filter using a WHERE clause if we want to process a specific row. However, there might be times we want to process each row sequentially, such as when we’re iterating an array. Unfortunately, there are no for loops in T-SQL. What we do have at our disposal, however, are T-SQL cursors.

T-SQL cursor

A cursor is a temporary object which allows us to iterate through the rows of a SELECT statement. It is very similar to a foreach loop in other programming languages.

Syntax

There is a specific procedure for creating and using cursors, which ...