主题:[讨论]急!触发器游标问题
我创建拉三个表
CREATE TABLE Student(
Sno CHAR(9) PRIMARY KEY,
Sname CHAR(20) UNIQUE,
SSex CHAR(2),
Sage SMALLINT,
Sdept CHAR(20),
AVGE int
)
CREATE TABLE Course(
Cno CHAR(4) PRIMARY KEY,
Cname CHAR(40),
Cpno CHAR(4),
Ccredit SMALLINT,
)
CREATE TABLE SC(
Sno CHAR(9),
Cno CHAR(4),
Grade int,
PRIMARY KEY(Sno,Cno),
FOREIGN KEY (Sno) REFERENCES Student(Sno),
FOREIGN KEY (Cno) REFERENCES Course(Cno)
)
insert into Student values('200215121','李勇','男',20,'CS',null)
insert into Student values('200215122','刘晨','女',19,'CS',null)
insert into Student values('200215123','王敏','女',18,'MA',null)
insert into Student values('200215124','张立','男',19,'IS',null)
insert into Course values('1','数据库','5',4)
insert into Course values('2','数学',null,2)
insert into Course values('3','信息系统','1',4)
insert into Course values('4','操作系统','6',3)
insert into Course values('5','数据结构','7',4)
insert into Course values('6','数据处理',null,2)
insert into Course values('7','PASC语言','6',4)
insert into SC values('200215121','1',92)
insert into SC values('200215121','2',85)
insert into SC values('200215121','3',88)
insert into SC values('200215122','2',90)
insert into SC values('200215122','3',80)
这里定义拉一个触发器
create trigger insertsql
on sc for insert
as
declare @num int,@temp int
select @num=d.sno from inserted d
set @temp=(select avg(grade) from sc where sno=@num)
if exists(select * from student where sno=@num)
begin
update student
set avge=@temp where sno=@num
end
else
begin
print '没有这个学生'
rollback transaction
end
/*update student set avge=(select avg(Grade) from sc where sno=@num) where sno=@num*/
go
需要请教的是,同时插入多条数据的时候,需要用游标,请问怎么改?谢谢
CREATE TABLE Student(
Sno CHAR(9) PRIMARY KEY,
Sname CHAR(20) UNIQUE,
SSex CHAR(2),
Sage SMALLINT,
Sdept CHAR(20),
AVGE int
)
CREATE TABLE Course(
Cno CHAR(4) PRIMARY KEY,
Cname CHAR(40),
Cpno CHAR(4),
Ccredit SMALLINT,
)
CREATE TABLE SC(
Sno CHAR(9),
Cno CHAR(4),
Grade int,
PRIMARY KEY(Sno,Cno),
FOREIGN KEY (Sno) REFERENCES Student(Sno),
FOREIGN KEY (Cno) REFERENCES Course(Cno)
)
insert into Student values('200215121','李勇','男',20,'CS',null)
insert into Student values('200215122','刘晨','女',19,'CS',null)
insert into Student values('200215123','王敏','女',18,'MA',null)
insert into Student values('200215124','张立','男',19,'IS',null)
insert into Course values('1','数据库','5',4)
insert into Course values('2','数学',null,2)
insert into Course values('3','信息系统','1',4)
insert into Course values('4','操作系统','6',3)
insert into Course values('5','数据结构','7',4)
insert into Course values('6','数据处理',null,2)
insert into Course values('7','PASC语言','6',4)
insert into SC values('200215121','1',92)
insert into SC values('200215121','2',85)
insert into SC values('200215121','3',88)
insert into SC values('200215122','2',90)
insert into SC values('200215122','3',80)
这里定义拉一个触发器
create trigger insertsql
on sc for insert
as
declare @num int,@temp int
select @num=d.sno from inserted d
set @temp=(select avg(grade) from sc where sno=@num)
if exists(select * from student where sno=@num)
begin
update student
set avge=@temp where sno=@num
end
else
begin
print '没有这个学生'
rollback transaction
end
/*update student set avge=(select avg(Grade) from sc where sno=@num) where sno=@num*/
go
需要请教的是,同时插入多条数据的时候,需要用游标,请问怎么改?谢谢