一个触发器来写的难题
用触发器来做
当对SC表插入、修改成绩时(只有对成绩),自动对sdate 列记录修改成绩的时间。
sc表只包括以下内容:
主码是:学号+课程号
sno(学号 char(5)),cno(课程号 char(1)),grade(成绩 decmial(3,1)),sdate(smalldatetime)
很急,在线等,我只做了插入的,但是没有结果,语法没问题,请高手帮忙写一下,谢谢
create trigger xp
on sc
for insert
as
if update(grade)
begin
select sdate=getdate()
from sc,inserted
where sc.sno=inserted.sno and sc.cno=inserted.cno
end
这是我写的,但不正确
问题点数:20、回复次数:16Top
1 楼xiaolovs()回复于 2005-06-02 18:38:18 得分 0
Create Table SC
(Sno char(5) not null,
Cno char(1) not null,
Grade decimal(3,1)
constraint PK_SC
primary key(sno,cno)
)
这是建表语句Top
2 楼yjdn(人形机器)回复于 2005-06-02 18:40:00 得分 3
create trigger tri_test
on sc
for insert,update
as
declare @sno char(5)
select @sno=sno from inserted
update sc set sdate=getdate() where sno=@snoTop
3 楼yjdn(人形机器)回复于 2005-06-02 18:42:40 得分 1
sno(学号 char(5)),cno(课程号 char(1)),grade(成绩 decmial(3,1)),sdate
--学号为5位是不是少了点?几年以后,你那个还够用吗?
还有,课程号,只有一位,如果课程数据大于10呢?Top
4 楼yjdn(人形机器)回复于 2005-06-02 18:43:14 得分 2
最好能给点数据测试Top
5 楼zengzhengliang(啊牛)回复于 2005-06-02 18:49:24 得分 3
create trigger xp
on sc
for insert,update
as
update sc
set
sdate=getdate()
where id = (select id from inserted)
不好意思,没调试,
为什么你不用一个标识呢?Top
6 楼xiaolovs()回复于 2005-06-02 19:33:52 得分 0
insert into sc
(sno,cno,grade)
values
('0301','1',90)
insert into sc
(sno,cno,grade)
values
('0302','2',80)
insert into sc
(sno,cno,grade)
values
('0301','01',100)
这些数据可以测试
Top
7 楼xiaolovs()回复于 2005-06-02 19:34:28 得分 0
因为这只是我的学校实验,我在做一个项目会用多点位数的,谢谢Top
8 楼xiaolovs()回复于 2005-06-02 19:36:46 得分 0
在线等,急,谢谢各位高手Top
9 楼paoluo(一天到晚游泳的鱼)回复于 2005-06-02 19:50:49 得分 5
create trigger xp
on sc
for insert,Update
as
if update(grade)
begin
Update A Set sdate=getdate()
from sc A,inserted B
where A.sno=B.sno and A.cno=B.cno
end
Top
10 楼paoluo(一天到晚游泳的鱼)回复于 2005-06-02 19:53:35 得分 5
--建立测试环境
Create Table SC
(Sno char(5) not null,
Cno char(1) not null,
Grade decimal(5,1),
sdate DateTime
constraint PK_SC
primary key(sno,cno)
)
GO
--建立触发器
create trigger xp
on sc
for insert,Update
as
if update(grade)
begin
Update A Set sdate=getdate()
from sc A,inserted B
where A.sno=B.sno and A.cno=B.cno
end
GO
--测试
insert into sc
(sno,cno,grade)
values
('0301','1',90)
insert into sc
(sno,cno,grade)
values
('0302','2',80)
insert into sc
(sno,cno,grade)
values
('0301','3',100)
GO
Select * from sc Order By Sno,Cno
Update sc Set grade=88 Where Sno='0302'
GO
Select * from sc Order By Sno,Cno
GO
--删除测试环境
Drop table sc
GO
--结果
/*
--插入数据后
Sno Cno Grade sdate
0301 1 90.0 2005-06-02 19:53:14.480
0301 3 100.0 2005-06-02 19:53:14.480
0302 2 80.0 2005-06-02 19:53:14.480
--更新数据后
0301 1 90.0 2005-06-02 19:53:14.480
0301 3 100.0 2005-06-02 19:53:14.480
0302 2 88.0 2005-06-02 19:53:14.490
*/Top
11 楼xiaolovs()回复于 2005-06-02 19:54:57 得分 0
现在做出来的都是错的,当然没有插入成绩时,sdate列应该默认为空的Top
12 楼xiaolovs()回复于 2005-06-02 20:05:35 得分 0
: paoluo(一天到晚游泳的鱼)
你的问题是:当只插入了学号和课程号,成绩为空时,sdept也有时间了,不合题意Top
13 楼xiaolovs()回复于 2005-06-02 20:10:32 得分 0
问题解决了,加个条件 A.grade is not null 就OK了Top
14 楼paoluo(一天到晚游泳的鱼)回复于 2005-06-02 20:16:50 得分 1
--建立测试环境
Create Table SC
(Sno char(5) not null,
Cno char(1) not null,
Grade decimal(5,1),
sdate DateTime
constraint PK_SC
primary key(sno,cno)
)
GO
--建立触发器
create trigger UpdateXp
on sc
for Update
as
if update(grade)
begin
Update A Set sdate=getdate()
from sc A,inserted B
where A.sno=B.sno and A.cno=B.cno
end
GO
create trigger insertxp
on sc
for insert
as
begin
Update A Set sdate=getdate()
from sc A,inserted B
where A.sno=B.sno and A.cno=B.cno And B.grade Is Not Null
end
GO
--测试
insert into sc (sno,cno,grade) values('0301','1',Null)
Select * from sc Order By Sno,Cno
GO
Update sc Set grade=88 Where Sno='0301'
Select * from sc Order By Sno,Cno
GO
--删除测试环境
Drop table sc
--结果
/*
Sno Cno Grade sdate
0301 1 NULL NULL
Sno Cno Grade sdate
0301 1 88.0 2005-06-02 20:16:22.243
*/Top
15 楼paoluo(一天到晚游泳的鱼)回复于 2005-06-02 20:17:16 得分 0
呵,对,我想复杂了。Top
16 楼xiaolovs()回复于 2005-06-02 20:25:59 得分 0
是的啊,你想的太多了,呵呵,不过你的想法没错,谢谢你Top




