Working with Cursors
In order to understand how to use a cursor, we will build a T-SQL script that uses a cursor, one step at a time, explaining the process as we go. The example will read rows from the Products table. If the product is from Germany, it will add 25% to the unit cost of the product. It will also produce a report of all unit costs and any changes that were made.
Note
The final code for this example can be found at the end of this section and on the CD-ROM.
Declaring the Cursor
The first step you must perform is to declare or create the cursor, associating it with a SELECT statement. The syntax of the DECLARE statement used to create a cursor is
Declare <cursor name> [insensitive] [scroll] cursor For <select statement> [for <read ...
Get Sams Teach Yourself Transact-SQL in 21 Days, Second Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.