AFTER UPDATING a trigger using the aggregate from the updated table

I am having problems with an update trigger. I want the trigger to set Quarterbacks.Yardsequal to the sum of the broadcast yards if they are in the same team.

create trigger T_Receiving_Passing
on NFL.Widereceivers
after update
as
begin
declare
@receivingyards int,

select @receivingyards = sum (Widereceivers.Yards) from NFL.Widereceivers
update NFL.Quarterbacks
set Quarterbacks.Yards = @receivingyards
where Quarterbacks.Team = Widereceivers.Team
end

At the end of the instruction it is Widereceivers.Teamunderlined in red and causes errors. I get the same error whenever I try to reference a column in another table without specifying the table in the sentence from. How can I fix this problem?

+4
source share
1 answer

, SELECT UPDATE, inserted, .

CREATE TRIGGER T_Receiving_Passing
ON NFL.Widereceivers
AFTER UPDATE
AS
  UPDATE NFL.Quarterbacks
  -- Get the SUM() in a subselect
  SET Quarterbacks.Yards = (SELECT SUM(Yards) FROM Widereceivers WHERE Team = inserted.Team) 
  FROM 
    NFL.Quarterbacks
    -- Join against the special inserted table
    JOIN inserted ON Quarterbacks.Team = inserted.Team
GO

SELECT . UPDATE , SET .

SUM(), , inserted, SET Yards = inserted.Yards. , Team, inserted.

inserted/deleted, . SQL Server , , inserted FROM, , JOIN ed. UPDATE inserted , , .

+3

All Articles