Click here to Skip to main content
Click here to Skip to main content

Always close your cursor

By , 13 Oct 2011
 
An alternative (simpler way :) ) is:
 
 
BEGIN TRAN
 
DECLARE IdCursor CURSOR FOR SELECT Id FROM MyTable
            
OPEN IdCursor
        
FETCH NEXT FROM IdCursor INTO @CurrentId
WHILE (@@FETCH_STATUS) = 0
    BEGIN   
        
    -- do work               
     IF @@error <> 0
     BEGIN 
      CLOSE IdCursor
      DEALLOCATE IdCursor    
      GOTO err
     END
                    
    FETCH NEXT FROM  IdCursor INTO @CurrentId
END -- end cursor

CLOSE IdCursor
DEALLOCATE IdCursor	
			
   
COMMIT TRAN
RETURN 0 -- success

err:
 
ROLLBACK TRAN 
RETURN 1   -- error

License

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
Member
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 | :)

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130523.1 | Last Updated 13 Oct 2011
Article Copyright 2011 by Elina Blank
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid