首页 新闻 会员 周边 捐助

SQL 触发器问题

0
[已关闭问题] 解决于 2012-10-19 01:13

--选课表
CREATE TABLE SC
(
Sno char(10) NOT NULL,
Cno char(10) NOT NULL,
SCfalg bit,
Sgrade smallint,
Snumber smallint,
PRIMARY KEY(Sno, Cno),
FOREIGN KEY(Sno) REFERENCES Student(Sno),
FOREIGN KEY(Cno) REFERENCES Course(Cno)
);

 

--创建一个触发器,当在选课表SC中插入相应课程号的记录时
--把选该课的学生人数加1
GO
CREATE TRIGGER Insert_SC ON SC
FOR INSERT
AS BEGIN
 IF (new.Cno = old.Cno) THEN
  new.Snumber = old.Snumber + 1;
 END IF;
END;

 

消息 156,级别 15,状态 1,过程 Insert_SC,第 4 行
关键字 'THEN' 附近有语法错误。

ageovb的主页 ageovb | 菜鸟二级 | 园豆:202
提问于:2012-05-13 11:54
< >
分享
其他回答(2)
0

THEN换成begin。

例如:

    USE pubs IF (SELECT AVG(price) FROM titles WHERE type = 'mod_cook') < $15 BEGIN PRINT 'The following titles are excellent mod_cook books:' PRINT ' ' SELECT SUBSTRING(title, 1, 35) AS Title FROM titles WHERE type = 'mod_cook' END ELSE PRINT 'Average title price is more than $15.'
悟行 | 园豆:12559 (专家六级) | 2012-05-13 12:47
0
    USE pubs IF (SELECT AVG(price) FROM titles WHERE type = 'mod_cook') < $15 BEGIN PRINT 'The following titles are excellent mod_cook books:' PRINT ' ' SELECT SUBSTRING(title, 1, 35) AS Title FROM titles WHERE type = 'mod_cook' END ELSE PRINT 'Average title price is more than $15.'你分开写吧,不然不好看
仗剑天涯人 | 园豆:38 (初学一级) | 2012-05-13 14:55
0

CREATE TRIGGER Insert_SC ON SC
FOR INSERT
AS BEGIN
 IF (new.Cno = old.Cno)
 BEGIN
 update new.Snumber set new.Snumber=old.Snumber + 1;
 END
END;

小材小用 | 园豆:639 (小虾三级) | 2012-05-14 09:02

消息 4104,级别 16,状态 1,过程 Insert_SC,第 68 行
无法绑定由多个部分组成的标识符 "new.Cno"。
消息 4104,级别 16,状态 1,过程 Insert_SC,第 68 行
无法绑定由多个部分组成的标识符 "old.Cno"。

支持(0) 反对(0) ageovb | 园豆:202 (菜鸟二级) | 2012-05-14 12:03

@lhui: 是不是还要用什么存储过程啊

支持(0) 反对(0) ageovb | 园豆:202 (菜鸟二级) | 2012-05-15 11:22
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册