From the course: Hands-On Advanced SQL Server: Strategies and Techniques

Unlock this course with a free trial

Join today to access over 25,600 courses taught by industry experts.

Cursors

Cursors

- In this lesson, we are exploring cursors, a way to go through query results one row at a time. Most of the time in SQL, we work with sets of data, but sometimes we need to do something special with each individual row, like build a string, run a row level logic, or step through records with fine control. If you ever needed to work with one record at a time instead of entire tables all at once, then you're at the right place. You see, in SQL Server, that's where cursors come in. They're a way to walk through a result set row by row, kind of like a loop in a traditional programming language. So, let's take a look at how they work, when to use them, and how to avoid their common pitfalls. Okay, let's start by setting up a couple variables to hold each row as we loop through the data. That's our first step right here. At this point, everything should seem pretty familiar. We're just setting up a few placeholders. MovieID will hold the ID and the Title will hold the name of each movie as…

Contents