H
hehe
Unregistered / Unconfirmed
GUEST, unregistred user!
我在GRADE 表中定义如下的触发器:
(其作用是当GRADE表中的数据发生变化的时候,重新计算各科总分,然后排序)
CREATE TRIGGER G_order ON grade
FOR UPDATE
AS
begin
update grade set
total =s1+s2+s3+s4+s5+s6+s7+s8+s9+s10+s11
--定义游标
declare G_order_cur scroll cursor for
Select Grade.Total,Grade.GradeOrder,Grade.Studentno,student.stuno,student.classno
from grade,student,classinfor,inserted
where grade.studentno=student.stuno and
student.classno=classinfor.classno and
classinfor.grade in
(select classinfor.grade from classinfor,student,Grade
where inserted.studentno=student.stuno and student.classno=classinfor.classno )
and Grade.term=inserted.term and Grade.test=inserted.test
order by Grade.total desc
--打开游标
open G_order_Cur
declare
@P_total int,
@N_order int,
@N_total int,
@n int,
@P_order int
--排序操作
select n=1
update Grade set Gradeorder=@n where current of G_order_Cur
fetch first from G_order_cur into @P_Total,@p_order
loop:
begin
select @n=@n+1
if @N_total=@p_total
update Grade set Gradeorder=@P_order where current of G_order_Cur
else
update Grade set Gradeorder=@n where current of G_order_Cur
fetch next from G_order_cur into @N_Total,@N_order
if @@fetch_Status=0 goto loop
end
--关闭游标
close G_order_cur
--删除游标
deallocate G_order_cur
end
当我在SQl Server Query Anasyse中执行以下语句
update grade set s8=9
其显示的结果为
(60 row(s) affected)
n
-----------
1
(1 row(s) affected)
Server: Msg 16929, Level 16, State 1, Procedure G_order, Line 25
The cursor is READ ONLY.
The statement has been terminated.
请教各位大虾是何原因?
(另外,还想请教怎样才能使当有多行数据更新时,
使触发器只执行一次(当然是最后一次执行)以提高效率)
(其作用是当GRADE表中的数据发生变化的时候,重新计算各科总分,然后排序)
CREATE TRIGGER G_order ON grade
FOR UPDATE
AS
begin
update grade set
total =s1+s2+s3+s4+s5+s6+s7+s8+s9+s10+s11
--定义游标
declare G_order_cur scroll cursor for
Select Grade.Total,Grade.GradeOrder,Grade.Studentno,student.stuno,student.classno
from grade,student,classinfor,inserted
where grade.studentno=student.stuno and
student.classno=classinfor.classno and
classinfor.grade in
(select classinfor.grade from classinfor,student,Grade
where inserted.studentno=student.stuno and student.classno=classinfor.classno )
and Grade.term=inserted.term and Grade.test=inserted.test
order by Grade.total desc
--打开游标
open G_order_Cur
declare
@P_total int,
@N_order int,
@N_total int,
@n int,
@P_order int
--排序操作
select n=1
update Grade set Gradeorder=@n where current of G_order_Cur
fetch first from G_order_cur into @P_Total,@p_order
loop:
begin
select @n=@n+1
if @N_total=@p_total
update Grade set Gradeorder=@P_order where current of G_order_Cur
else
update Grade set Gradeorder=@n where current of G_order_Cur
fetch next from G_order_cur into @N_Total,@N_order
if @@fetch_Status=0 goto loop
end
--关闭游标
close G_order_cur
--删除游标
deallocate G_order_cur
end
当我在SQl Server Query Anasyse中执行以下语句
update grade set s8=9
其显示的结果为
(60 row(s) affected)
n
-----------
1
(1 row(s) affected)
Server: Msg 16929, Level 16, State 1, Procedure G_order, Line 25
The cursor is READ ONLY.
The statement has been terminated.
请教各位大虾是何原因?
(另外,还想请教怎样才能使当有多行数据更新时,
使触发器只执行一次(当然是最后一次执行)以提高效率)