我可以在这种情况下使用触发器吗?

时间:2021-03-02 08:41:10

I have a table (ie. ReportData) which contains some calculated data -> eg. classroom average scores, number of students, avg grade point, etc. etc. lots of report like data. Works great.

我有一个表(即ReportData),其中包含一些计算数据 - >例如。课堂平均分数,学生人数,平均分等等。大量的报告就像数据一样。效果很好。

Now, in some of my other tables (eg. the ClassRoom table) if i change some data in there, I need to update the ReportData table.

现在,在我的一些其他表(例如ClassRoom表)中,如果我在那里更改了一些数据,我需要更新ReportData表。

So I thought that if i do any INSERT, UPDATE or DELETE on the ClassRoom table, then i can run my stored procedure which updates the ReportData table, data.

所以我认为如果我在ClassRoom表上执行任何INSERT,UPDATE或DELETE,那么我可以运行更新ReportData表数据的存储过程。

SO i thought a trigger would be good for this. But, I'm not sure. Why? Well, the stored procedure which is used to do all the custom math will actually occasionaly query the ClassRoom table. So will it access the most recent (eg. Changed) data?

所以我认为触发器对此有好处。但是,我不确定。为什么?那么,用于执行所有自定义数学运算的存储过程实际上偶尔会查询ClassRoom表。那么它会访问最新的(例如更改的)数据吗?

eg. If i change the field 'NumberOfStudents' from a 4 -> 5. Will the stored procedure (which will end up checking that field, on the ClassRoom table, retrieve a 4 or 5 as the data?

例如。如果我从4 - > 5更改字段'NumberOfStudents'。存储过程(将最终检查该字段,在ClassRoom表上,检索4或5作为数据?

Thanks :)

NOTE: ClassRooms, etc are just fake for the purpose of this discussion.

注意:为了讨论的目的,ClassRooms等只是假的。

2 个解决方案

#1


The rule of transactions is that you can always see you own changes to the data.

事务规则是您始终可以看到自己对数据的更改。

#2


Maybe I have some misuderstanding on your question, but SQL Server 2008 has two types of triggers: AFTER (that fires after the changes will applied) and INSTEAD OF (you can do what you want before of changes of the table applied). If you want to update the ClassRoom table and then recalculate the ReportData table using new values - just use AFTER trigger.

也许我对你的问题有一些误解,但是SQL Server 2008有两种类型的触发器:AFTER(在应用更改后触发)和INSTEAD OF(你可以在更改应用的表之前做你想做的事)。如果要更新ClassRoom表,然后使用新值重新计算ReportData表 - 只需使用AFTER触发器。

#1


The rule of transactions is that you can always see you own changes to the data.

事务规则是您始终可以看到自己对数据的更改。

#2


Maybe I have some misuderstanding on your question, but SQL Server 2008 has two types of triggers: AFTER (that fires after the changes will applied) and INSTEAD OF (you can do what you want before of changes of the table applied). If you want to update the ClassRoom table and then recalculate the ReportData table using new values - just use AFTER trigger.

也许我对你的问题有一些误解,但是SQL Server 2008有两种类型的触发器:AFTER(在应用更改后触发)和INSTEAD OF(你可以在更改应用的表之前做你想做的事)。如果要更新ClassRoom表,然后使用新值重新计算ReportData表 - 只需使用AFTER触发器。