Cursor - Close vs Deallocate in SQL
Apart from main difference between close and deallocate Cursor, here are couple of more points to remember them :1) Deallocating a cursor automatically closes it, the reverse is not true.
2) Closing an open cursor, only releases current result set and free up any cursor locks held on rows, on which the cursor is positioned while deallocate completely removes cursor reference.
3) You can reopen a close cursor and can perform the fetch operation on that, this is not possible once you deallocate a cursor. For example, following is legal :
close employee_cursor
open employee_cursor
You can then fetch from employee_cursor, as a normally opened cursor. In Sybase Adaptive Server, any conditions associated with a cursor, remains in effect, even after reopening a closed cursor.
fetch employee_crsr
emp_id emp_name age
----------- ------------------- ---------------
16032243 John 31
16032243 Johnny 32
16032243 Robin 33
deallocate employee_cursor;
4) In Microsoft SQL Server, when the last cursor reference is deallocated, the data structures comprising the cursor are released.

That's all on the difference between closing or deallocating a Cursor in SQL. It's SQL best practice to not only close but also deallocate any opened cursor so that all the associated resource can be released and reused.
No comments:
Post a Comment