SQL Server Cursor Review

Source: Internet
Author: User

Often write stored procedures, but today in the use of cursors or negligence of some things, the implementation of the process has not been carried out, and then directly linked to SQL Server, the lesson Ah!

Although the code is simple, I hope to remember:

Create PROCEDURE [dbo]. [TEMPHXB] Asbegin Declare @uidintDeclare mycursortemp Cursor for SelectUid fromTemptable1whereIndate>'2015-05-21'and type='1'GROUP BY UID have COUNT (*) >1Open mycursortemp Fetch next frommycursortemp into @uid while@ @FETCH_STATUS =0begin Delete fromTemptable1whereIDinch(SelectTop1Id fromTemptable1where[email protected] and indate>'2015-05-21'and fdtype='1') Update Temptable2Setnum=num+1 whereUid=@uid FETCH Next frommycursortemp into @uid end close mycursortemp deallocate mycursortemp end

Cursors are generally divided into the following five steps during use:
1. Declaring cursors

2. Open cursor

3. Using Cursors

4. Close the cursor

5. Deleting cursors

Today I was in the third step of the cursor traversal data to forget to execute the fetch next from the mycursortemp into @uid, resulting in a dead loop, resulting in a small loss.
Many others forget to close the cursor, which also causes the next execution error.

Database operations need to be cautious and remember.

SQL Server Cursor Review

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.