USE [nmgdl] GO /****** Object: Trigger [dbo].[trg_NonExamCourse] Script Date: 02/28/2015 11:31:02 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create TRIGGER [dbo].[trg_NonExamCourse] ON [dbo].[Relation_User_Course] FOR UPDATE AS IF UPDATE(StudyTimes) BEGIN DECLARE @jdconfig decimal(18,0), --课程进度参数(Course表中值) @jdstudy decimal(18,0), --已经学习的进度 @totalscore1 decimal(18,1), --一门课程的学分 @coursetime1 decimal(18,1), --一门课程的总课时长 @userid1 nvarchar(36), --用户ID @courseid1 nvarchar(36), --课程ID @gradeid1 nvarchar(36), --班级ID @ispassed INT,--是否已经课程通过 @totalscore2 decimal(18,1),--该学员已经取得的学分 @coursetime2 decimal(18,1) --该学员已经学习的总时长 SELECT @jdconfig=t.IsExam,@totalscore1=t.CourseScore,@coursetime1=t.CourseTime, @jdstudy=t2.StudyTimes,@userid1=t2.userid,@courseid1=t2.courseid,@ispassed=t2.Passed,@gradeid1=t2.GradeID FROM dbo.Course t,INSERTED t2 WHERE t.ID=t2.CourseID AND t.PassMethod=1; SELECT @totalscore2=TotalScore,@coursetime2=TotalCourseTime FROM dbo.Relation_Grade_User WHERE GradeID=@gradeid1 AND UserID=@userid1; IF(@jdconfig<=@jdstudy) BEGIN IF(@ispassed=0 or @ispassed is null) BEGIN UPDATE dbo.Relation_Grade_User SET TotalScore=@totalscore1+@totalscore2,TotalCourseTime=@coursetime1+@coursetime2 WHERE UserID=@userid1 AND GradeID=@gradeid1; UPDATE dbo.Relation_User_Course SET Passed=1 WHERE UserID=@userid1 AND CourseID=@courseid1; END END END