Visual Studio SQL Server Insert Statement -
this trigger. first 2 update statements work but, update student not work.
"update student set pointsaccumulated = pointsaccumulated + @points @studentid = studentid" create trigger [trigger] on [dbo].[attendance] after insert begin declare @eventid int declare @studentid nchar declare @points int select @eventid=eventid inserted select @eventid = eventid, @points = points event select @studentid = studentid inserted update event set attendance = attendance + 1 @eventid = eventid update attendance set points = @points @eventid = eventid update student set pointsaccumulated = pointsaccumulated + @points @studentid = studentid
the solution worked me hlgem :
update e set attendance = attendance + 1 event e join inserted on i.eventid = e.eventid update set points = e.points attendance join event e on a.eventid = e.eventid join inserted on i.eventid = e.eventid update s set pointsaccumulated = pointsaccumulated + e.points student s join inserted on i.studentid = s.studentid join event e on i.eventid = e.eventid
lessons learned: in sql server, never appropriate use variable (other table variable) grab changed in table in trigger not work row-by row triggers in other databases --hlgem
this not tested, think need:
create trigger [trigger] on [dbo].[attendance] after insert begin update e set attendance = attendance + 1 event e join inserted on i.eventid = e.eventid update set points = e.points attendance join event e on a.eventid = e.eventid join inserted on i.eventid = e.eventid update s set pointsaccumulated = pointsaccumulated + e.points students s join inserted on i.studentid = e.studentid join event e on i.eventid = e.eventid
note fixes incorrect first 2 updates well. when writing sql server trigger, must assume there more 1 record in inserted or deleted. further, need test trigger deliberately making sure multiple records affected initial insert or update. irresponsible create trigger assumption 1 record @ time ever inserted, updated or deleted. , if don;t test behavior that, have trigger creates data integrity disaster tables have multiple record insert/update or deletion.
Comments
Post a Comment