How to share SQL cursor instances

Source: Internet
Author: User

[SQL]
-- 1. Update the salary of each teacher to the original salary + bonus

-- Defines two variables to store ttid and reward

Declare @ tid int
Declare @ reward money

-- 1. Create a cursor Based on the bonus table

Declare cur_reward cursor fast_forward for select ttid, reward from TblTeacherSalary
-- 2. Open the cursor
Open cur_reward

-- Read data through a cursor
Fetch next from cur_reward into @ tid, @ reward
While @ fetch_status = 0
Begin

-- Update salary
Update TblTeacher set ttsalary = ttsalary + @ reward where ttid = @ tid
Fetch next from cur_reward into @ tid, @ reward
End

-- 3. Close the cursor
Close cur_reward

-- 4. release resources
Deallocate cur_reward

Note: In general, do not use a cursor. Extremely low performance. Assume that you are processing a large amount of data. When the execution of common SQL statements is very slow, you can try the cursor at this time. It may bring unexpected results to you.


Author xhccom

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.