這是我的觸發器。前兩個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