Many times we get a result set for SQL operations, and when we need to look at the contents of the collection in turn, we need the cursor attributes.
So, a SQL cursor is like an iterator to a container in C + +, and here's an example to help understand:
DECLAREVend_cursorCURSOR for SELECT * fromPurchasing.vendorOPENVend_cursorFETCH NEXT fromVend_cursor while(@ @fetch_status=0) BEGIN //Balabala ...FETCH NEXT fromVend_cursorENDCLOSEVend_cursordeallocateVend_cursor
Code Analysis: First use DECLARE to declare the cursor, which is the result set, then open the cursor, use the cursor, close the cursor, release the cursor.
Fetch obtains data from the cursor, while the condition of the while is the global variable (@@ 表示) Fetch_status, indicating whether the running result state of the most recent fetch was successful, or 0, which means that the data was successfully obtained.
T-SQL cursors and Fetch