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