T-SQL cursors and Fetch

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.