更新触发器影响太多行

时间:2013-12-21 03:31:24

标签: sql

我写了以下触发器:

begin

update NFL.TeamStatistics

set Passing_Yards = (select sum(Quarterbacks.Yards) 
                     from NFL.Quarterbacks 
                     where Quarterbacks.Team = inserted.Team)

from NFL.Quarterbacks 
            inner join inserted on Quarterbacks.Team = inserted.Team;

每当有人更新表格中关于四分卫的传球码数时,它应自动将NFL.TeamStatistics.Passing_Yards设置为每个球队传球码数的总和。

我使用以下更新语句来测试它:

update NFL.Quarterbacks

set Quarterbacks.Yards = 4000

where Team = 'PIT';

但是,在表NFL.TeamStatistics中,它将所有团队的passingyards设置为4000而不仅仅是PIT。怎么了?

2 个答案:

答案 0 :(得分:1)

您需要在NFL.TeamStatistics中添加要与之比较的where子句和列

e.g:

 begin

update NFL.TeamStatistics

set Passing_Yards = (select sum(Quarterbacks.Yards) 
                     from NFL.Quarterbacks 
                     where Quarterbacks.Team = inserted.Team)

from NFL.Quarterbacks 
            inner join inserted on Quarterbacks.Team = inserted.Team
where Team = Quarterbacks.Team;

假设您正在与Quaterbacks.Team进行比较

答案 1 :(得分:0)

您应该在update语句中使用where子句,以便过滤您要更新的数据。