Update the inserted SQL transaction statement using the cursor Loop

Source: Internet
Author: User
-- Start transaction
Begin tran

-- Do Not Display count Information
Set nocount on

Declare @ userid varchar (512)
Declare @ A int
Set @ A = 10000

-- Declare a cursor
Declare crmpscontact_cursor cursor
Select [userid] from [gmcc]. [DBO]. [cvv_users]

-- Open the cursor
Open crmpscontact_cursor

-- Return the value of the first row to the variable.
Fetch next from crmpscontact_cursor
Into @ userid

-- Execute the statement
Update [gmcc]. [DBO]. [cvv_users]
Set [workid] = @
Where [userid] = @ userid
Set @ A = @ A + 1

-- Execution error rollback
If @ error! = 0
Begin
Rollback tran
Return
End

-- Move the cursor and update all other rows (exit when the cursor ends)
While @ fetch_status = 0
Begin
-- Move the cursor to the next row
Fetch next from crmpscontact_cursor
Into @ userid

-- Execute the statement, starting from the second line
Update [gmcc]. [DBO]. [cvv_users]
Set [workid] = @
Where [userid] = @ userid

Set @ A = @ A + 1

-- Execution error rollback
If @ error! = 0
Begin
Rollback tran
Return
End

End

-- Close the cursor
Close crmpscontact_cursor

-- Release cursor
Deallocate crmpscontact_cursor

-- Submit all changes
Commit tran

-- Restore settings
Set nocount off
Go

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.