我在创建触发器时遇到困难,该触发器更新了已注册的学生数量。以下是我的表格设置方式。第一段代码由我的导师提供。每当学生注册或被删除时,register_count表都需要使用适当的学生注册数量进行更新。
Create table Registered
(
ssn numeric,
code char(10),
year int,
semester char(10),
foreign key (ssn) references Student(ssn),
foreign key (code) references Course(code),
primary key (ssn,code,year,semester)
);
create table Register_Count
(
code char(10),
year int,
semester char(10),
count int default 0,
primary key (code, year, semester)
);
这是我到目前为止触发的尝试。我不断得到'多部分标识符无法绑定错误。'
CREATE TRIGGER updateCount ON registered
AFTER UPDATE, DELETE, INSERT
AS
UPDATE register_count SET
code = (select code from inserted),
year = (select year from inserted),
semester = (select semester from inserted),
count = (select count(*) from registered)
WHERE Register_Count.code = inserted.code
GO
答案 0 :(得分:0)
您可以拥有区分大小写的排序规则,您需要更改该行:
WHERE Register_Count.code = inserted.code
为:
WHERE register_count.code = inserted.code
在这种情况下,虽然您正在更新register_count,但您在WHERE子句中有一个对'inserted'的引用,您在UPDATE语句中没有引用它。
我想你想要这样的东西:
UPDATE register_count
from register_count inner join inserted
on Register_Count.code = inserted.code
SET
code = inserted.code,
year = inserted.year,
semester = inserted.semester,
count = (select count(*) from registered)