Learning and using ms SQL Server CURSOR

Source: Internet
Author: User

To be honest, the cursor has never been used since ms SQL Server was used for such a long time. Previously, similar functions were implemented using a WHILE loop and a temporary table. I wrote a reference to the online example exercise just now. Learn about the cursor concept and syntax.

In the following code example,
First, declare that you need to use variables in the cursor, that is, temporarily store data that processes fields.
2. Declare a cursor and SELECT the dataset to be processed.
3. Open the cursor (#8 lines of code ).
4. Use fetch next data from the cursor to assign values to the variable.
5. Loop @ FETCH_STATUS = 0.
6. In the loop block, you can process the first record logic. In this example, PRINT is used.
7. After the logic processing is completed, fetch next data is taken from the cursor again to assign a value to the variable. (Same as above 4 ).
8. Close the cursor.
9. Delete and release the cursor.

 

Execution result:

Related Article

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.