我需要创建一个系统,该过程使用过程和游标来计算给定类型的工作(见陈述)的员工薪水,但是当执行游标时,它将继续加载,而当取消游标时,它将更新表并设置所有值与存储过程中的第一个if语句相同
create procedure salaryproc @booking_type varchar(50), @staff_type varchar(50), @salary int = 0
as
begin
if(@booking_type='personal' and @staff_type='booking')
begin
set @salary = 1500+2300
end
if(@booking_type='personal' and @staff_type='catering')
begin
set @salary = 1500+1900
end
if(@booking_type='official' and @staff_type='booking')
begin
set @salary = 1800+2300
end
if(@booking_type='official' and @staff_type='catering')
begin
set @salary = 1800+1900
end
update staff
set salary=@salary
end
declare @booking_type varchar(50)
declare @staff_type varchar(50)
declare @salary int
declare salary_cursor cursor
for select b.type, s.type, s.salary
from booking as b, staff as s
open salary_cursor
fetch next from salary_cursor
into @booking_type, @staff_type, @salary
while(@@fetch_status=0)
begin
exec salaryproc @booking_type , @staff_type, @salary
end
close salary_cursor
答案 0 :(得分:2)
salaryproc中的更新语句正在更新人员表中的每条记录。添加where语句以将其简化为您要更新的记录。
update staff
set salary=@salary
where
bookingType = @booking_type
and staffType = @staff_type
可以将游标和存储过程简化为设置基于更新的语句。基于集合的操作优于游标。
-- set the salary for personal + booking
update staff
set salary= 1500+2300
where
bookingType = 'personal'
and staffType = 'booking'
-- set the salary for personal + catering
update staff
set salary= 1500+1900
where
bookingType = 'personal'
and staffType = 'catering'
-- set the salary for official + booking
update staff
set salary= 1800+2300
where
bookingType = 'official'
and staffType = 'booking'
-- set the salary for official + catering
update staff
set salary= 1800+1900
where
bookingType = 'official'
and staffType = 'catering'
答案 1 :(得分:2)
我认为您可以只用一条语句进行更新
update staff
set salary =
case when bookingType = 'personal' and staffType = 'booking' then 1500+2300
when bookingType = 'personal' and staffType = 'catering' then 1500+1900
when bookingType = 'official' and staffType = 'booking' then 1800+2300
when bookingType = 'official' and staffType = 'catering' then 1800+1900
else salary
end
where bookingType in ('personal', 'official')
and stafftype in ('booking', 'catering')