Hello to all community,

I need to improve the performance of the following trigger:

CREATE trigger [ATTENDANCE_INSUPDDEL] on [dbo].[ATTENDANCE]
for update,insert,delete
as
declare @r int
declare cur cursor for select distinct registration from inserted union select distinct registration from deleted
open cur
fetch next from cur into @r
while @@fetch_status=0
begin
exec sync_attendance @r
fetch next from cur into @r
end
close cur
deallocate cur

elow is the code of sync_attendance:

thanks a lot for your time!

CREATE procedure [dbo].[sync_attendance] @registration int
as
declare @student int
declare @semester int
set @student=(select distinct student from registration where code=@registration)
set @semester=(select distinct semester from registration where code=@registration)
delete from _totalgrade where student=@student and semester=@semester
insert into _totalgrade select distinct * from fn_totalgrade(@student, @semester)
delete from _stud_progress where student=@student and psem>=@semester