0

我很难创建一个触发器来更新注册学生的数量。这是我的桌子的设置方式。第一个代码块是由我的导师提供的。每当一个学生注册或被删除时,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
4

1 回答 1

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)
于 2013-09-23T00:53:56.967 回答