Usage of SQL cursor

Source: Internet
Author: User

The table1 structure is as follows2Idint 3Namevarchar( -) 4  5 Declare @id int 6 Declare @name varchar( -) 7 DeclareCursor1cursor  for         --Defining Cursors Cursor1 8 Select *  fromTable1--objects using cursors (fill in select text as required) 9 OpenCursor1--Open CursorTen  One Fetch Next  fromCursor1 into @id,@name  --move the cursor down by 1 rows to get the data into the previously defined variable @id, @name A  -  while @ @fetch_status=0           --determine if the data was successfully obtained - begin the UpdateTable1SetName=Name+'1' - whereId=@id                           --Handle it accordingly (fill in the SQL text as needed) -  - Fetch Next  fromCursor1 into @id,@name  --move the cursor down by 1 rows + End -  + CloseCursor1--Close Cursors A deallocateCursor1

Cursor General format:
DECLARECursor nameCURSOR for SELECTField1,Field2,Field3,... FromTable nameWHERE ...
OPENCursor name
FETCH NEXT fromCursor nameIntoVariable name1,Variable name2,Variable name3,...
While @ @FETCH_STATUS =0
BEGIN
SqlStatement execution procedure... ...
FETCH NEXT fromCursor name1, Variable name 2, Variable name 3,...
        END
< Span style= "Font-family:times New Roman;" >close cursor name
deallocate Reference: http://www.cnblogs.com/Gavinzhao/archive/2010/07/14/ 1777644.html

Use of SQL cursor

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.