这是我的触发器。前两个UPDATE语句有效,但UPDATE Student不起作用。Visual Studio SQL Server插入语句
"UPDATE Student SET PointsAccumulated = PointsAccumulated + @Points
Where @StudentID = StudentID"
CREATE TRIGGER [Trigger]
ON [dbo].[Attendance]
After insert
AS
BEGIN
DECLARE @EventID int
Declare @StudentID nchar
DECLARE @Points int
SELECT @EventID=EventID from inserted
SELECT @EventID = EventID, @Points = Points from Event
SELECT @StudentID = StudentID from inserted
Update Event SET Attendance = Attendance + 1
WHERE @EventID = EventID
UPDATE Attendance SET Points = @Points
Where @EventID = EventID
UPDATE Student SET PointsAccumulated = PointsAccumulated + @Points
Where @StudentID = StudentID
是为我工作从HLGEM解决方案:
Update E
SET Attendance = Attendance + 1
FROM Event E
JOIN Inserted I ON i.eventid = e.eventid
UPDATE A
SET Points = e.points
FROM Attendance a
JOIN Event E ON a.EventID = e.EventID
JOIN Inserted I ON i.EventID = e.EventID
UPDATE S
SET PointsAccumulated = PointsAccumulated + e.points
FROM Student S
JOIN Inserted I ON i.studentID = s.studentID
JOIN Event E ON i.eventid = e.eventid
经验教训: 在SQL Server中,这是从来没有适合使用的变量(比表变量等)抢什么在表中改变了一个触发器,因为他们没有在其他一些数据库 --HLGEM
它是否产生任何错误或只是不更新?你能确保Where @StudentID = StudentID返回结果。 –
没有错误 – wes
您正在设置来自Event表的Points变量,并且没有where条件。我的心理猜测是,积分不包含你认为它包含的内容。如果没有where子句,它将被设置为表中其中一条记录的值......哪一个......谁知道! – Jeremy