[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