alter table subject
add DG nvarchar(20)
create proc FillDGSub
@subjectID varchar(50)
as
begin
if (not exists (select * from Subject where ID=@subjectID))
return
declare @sltd int
select @sltd=COUNT(*)
from result r
where r.subjectID=@subjectID
and r.Mark>=5 and r.times > = all
(select r1.times from result r1
where r1.studentID=r.StudentID and
r1.subjectID=r.SubjectID)
if(@sltd>=1)
begin
declare @min float
select @min=MIN(r.mark)
from result r
where r.subjectID=@subjectID
declare @dg nvarchar(20)
if(@min>5)
set @dg = N'đạt'
else
begin
set @dg= N'không đạt'
end
update subject set DG=@dg where ID=@subjectID
end
end
go
create proc FillSub
as
begin
declare c cursor for
(select ID from subject where credit=4)
open c
declare @ID varchar(50)
fetch next from c into @ID
while(@@fetch_status=0)
begin
exec FillDGSub @ID
fetch next from c into @ID
end
close c
deallocate c
end