Запрос продолжает выполняться, когда нажмите кнопку отмены, затем говорит, что строки затронуты - PullRequest
1 голос
/ 19 марта 2019

мне нужно создать систему, которая рассчитывает зарплату персонала для данного типа работы (см. Операторы) с использованием процедур и курсоров, но при исключении курсора он продолжает загружаться, а при отмене обновляет таблицу и устанавливает все значения к первому оператору 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

Ответы [ 2 ]

2 голосов
/ 19 марта 2019

Я думаю, что вы можете сделать свое обновление с одним утверждением

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')
2 голосов
/ 19 марта 2019

Оператор обновления в 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'
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...