SQL SERVER 触发器问题(100分)

  • 主题发起人 主题发起人 hehe
  • 开始时间 开始时间
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.
请教各位大虾是何原因?
(另外,还想请教怎样才能使当有多行数据更新时,
使触发器只执行一次(当然是最后一次执行)以提高效率)
 
我也想听
 
《数据库原理》一书中的标准答案:
如果游标定义中的select用了order by ,union,或相当于定义了
不可更新的视图,就不能用
update s set .... where current of sc
or
delete from ... where current of sc
:)
 
接受答案了.
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
I
回复
0
查看
3K
import
I
I
回复
0
查看
2K
import
I
I
回复
0
查看
3K
import
I
后退
顶部