Click here to Skip to main content
13,736,690 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


1 bookmarked
Posted 18 Oct 2011
Licenced CPOL

Always close your cursor

, 18 Oct 2011
To see the default value of the cursor scope:select * from sys.databases(if value of "is_local_cursor_default" is 0 then scope id global, otherwise it is local).The scope is GLOBAL by default (for compatibility reasons).To change default scope use following:ALTER DATABASE...


Members may post updates or alternatives to this current article in order to show different approaches or add new features.

13 Oct 2011
Elina Blank
An alternative (simpler way :) ) is:BEGIN TRANDECLARE IdCursor CURSOR FOR SELECT Id FROM MyTable OPEN IdCursor FETCH NEXT FROM IdCursor INTO @CurrentIdWHILE (@@FETCH_STATUS) = 0 BEGIN -- do work IF @@error 0...
18 Oct 2011
You also might want to look into the "LOCAL" keyword.This limits the scope of the cursor to the context in which it is created.DECLARE IdCursor CURSOR LOCAL FOR SELECT Id FROM MyTableThis is beneficial for many reasons.
13 Oct 2011
Elina Blank
3 alternatives  
When cursors are inside transaction, it is sometimes easy to forget to close the cursor


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Elina Blank
Software Developer (Senior) The Code Project
United States United States
Elina joined the Code Project team as a software developer in order to make the site run even smoother than it is now. She will also take care of making wishes and promises on the site improvements come to the light sooner than later. When not at work, she enjoys being with her family and wishes there will be at least 30 hours in a day Smile | :)

| | Privacy | Cookies | Terms of Use | Mobile
Web01-2016 | 2.8.180920.1 | Last Updated 18 Oct 2011
Article Copyright 2011 by Elina Blank
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid